gpt4 book ai didi

android - 将文本设置为 fragment 内的 TextView

转载 作者:行者123 更新时间:2023-11-29 21:45:12 26 4
gpt4 key购买 nike

我在一个 fragment 中有一个 ListView ,在另一个 fragment 中有一个 TextView 。我必须在 TextView 的 ListView 中显示数字的平方。但是,无论如何我都会得到零,即使单击列表中的项目,文本也不会改变。

这是代码...

MainActivity.Java

package com.example.fragmentdemo;

public class MainActivity extends Activity implements Prime.onItemListSelectedListener{

boolean mDualPane;
int n;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Check that the activity is using the layout version with
// the fragment_container FrameLayout
if (findViewById(R.id.fragment_container) != null) {

// However, if we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) {
return;
}

Prime fragmentPrime = new Prime();
fragmentPrime.setArguments(getIntent().getExtras());
getFragmentManager().beginTransaction()
.add(R.id.fragment_container, fragmentPrime).commit();
}


Square square = (Square) getFragmentManager().findFragmentById(R.id.fragment_content_2);

if(square != null)
{
// In Dual Pane Mode

square.setSquare(n);
}

else
{
Square newFragment = (Square) getFragmentManager().findFragmentById(R.id.fragment_content_2);

Bundle args = new Bundle();
args.putInt("number", n);
newFragment.setArguments(args);

FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();

}

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

@Override
public void onItemSelected(int number) {
// TODO Auto-generated method stub

n = number;
}

}

Prime.Java - 第一个 fragment

package com.example.fragmentdemo;

public class Prime extends Fragment{

ArrayList<String> alPrime = new ArrayList<String>();
ArrayAdapter<String> ad;

onItemListSelectedListener mCallback;

public interface onItemListSelectedListener{
public void onItemSelected(int number);
}


@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);


// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (onItemListSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement onItemListSelectedListener");
}
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub

View view = inflater.inflate(R.layout.fragment_prime_list, container, false);

setNumbers();

final ListView lv = (ListView) view.findViewById(R.id.lvPrime);
ad = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, alPrime);
lv.setAdapter(ad);

lv.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub

mCallback.onItemSelected(Integer.parseInt(lv.getItemAtPosition(arg2).toString()));
}
});

return view;
}

private void setNumbers() {
// TODO Auto-generated method stub
// Will replace this code with the logic of Prime Numbers later
for(int i = 2; i <= 10; i++)
{
alPrime.add(String.valueOf(i));
}
}

}

Square.Java - 第二个 fragment

package com.example.fragmentdemo;

public class Square extends Fragment{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub

View view = inflater.inflate(R.layout.fragment_prime_list, container, false);
return view;
}

public void setSquare(int number)
{
// The toast displays zero
Toast.makeText(getActivity(), String.valueOf(number), Toast.LENGTH_LONG).show();
TextView txtSquare = (TextView) getView().findViewById(R.id.txtViewSquare);
int square = number * number;

txtSquare.setText(String.valueOf(square));
}

}

最佳答案

当您的 Activity 收到 onItemSelected 事件时,它不仅应该存储数字,而且如果已创建 fragment ,还应将其传递给第二个 fragment ,否则,它应该创建 fragment 并通过其参数将数字 n 传递给它 bundle 。

@Override
public void onItemSelected(int number) {
// TODO Auto-generated method stub

n = number;

//remove this code from onCreate and put it here
Square square = (Square) getFragmentManager().findFragmentById(R.id.fragment_content_2);

if(square != null)
{
// In Dual Pane Mode
square.setSquare(n);
}
else
{
Square newFragment = Square.newInstance(n);

FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();
}
}

方形 fragment :

public Square newInstance( int n ) {
Square newFragment = new Square();
Bundle args = new Bundle();
//create a constant instead of the string 'number'
args.putInt("number", n);
newFragment.setArguments(args);
return newFragment;
}

关于android - 将文本设置为 fragment 内的 TextView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16052077/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com