我是使用CustomAdapter创建ListViews的新手(这是我的第一次尝试),我不确定我做得多好/高效。所以我想要做的就是在ListView中获得突出显示为红色的名称。我使用getSelectedName方法执行此操作。但是,在BuildInput类中,当我想要MainActivity并且意图回到BuildInput类时,ListView完全消失了。由于ListView已经消失,突出显示的红色名称和getSelectedName方法也不起作用。
当我打算使用MainActivity时,如何防止ListView进行减少?
public class Names {
public String name;
public Names(){
super();
}
public Names(String name){
super();
this.name = name;
-
public class CustomAdapter extends ArrayAdapter<Names>{
private ArrayList<Names> items;
private TextView tvHolder;
private String selectedName = "";
public CustomAdapter (Context context, ArrayList<Names> items){
super(context, 0, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Names names = getItem(position);
if(convertView == null)
{
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_namechoices, parent, false);
}
Names nm = items.get(position);
if (nm != null){
final TextView tv = (TextView)convertView.findViewById(R.id.nameItem);
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (tvHolder != null) {
tvHolder.setBackgroundColor(Color.WHITE);
v.setBackgroundColor(Color.RED);
tvHolder = tv;
selectedName = tv.getText().toString();
} else {
v.setBackgroundColor(Color.RED);
tvHolder = tv;
selectedName = tv.getText().toString();
}
}
});
}
TextView tv = (TextView)convertView.findViewById(R.id.nameItem);
tv.setText(names.name);
return convertView;
}
public String getSelectedName(){
return selectedName;
}
}
-
public class BoardInput extends Activity {
private EditText mUserInput;
private Button mConfirm;
private Button mReady;
private ArrayList<Names> arrayOfNames = new ArrayList<Names>();
private CustomAdapter adapter;
private ListView listview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_boardinput);
adapter = new CustomAdapter(BoardInput.this, arrayOfNames);
listview = (ListView) findViewById(R.id.listview);
listview.setAdapter(adapter);
mUserInput = (EditText) findViewById(R.id.nameInput);
mConfirm = (Button) findViewById(R.id.confirm);
mConfirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mUserInput.getText().toString().equals("")) {
Toast.makeText(getApplicationContext(), "Please enter a name.", Toast.LENGTH_SHORT);
} else {
arrayOfNames.add(new Names(mUserInput.getText().toString()));
mUserInput.setText("");
adapter = new CustomAdapter(BoardInput.this, arrayOfNames);
listview = (ListView) findViewById(R.id.listview);
listview.setAdapter(adapter);
}
}
});
mReady = (Button) findViewById(R.id.ready);
mReady.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if (adapter.getSelectedName().isEmpty()){
Log.d("NULL","NULL");
}
else{
Log.d("BoardInput", adapter.getSelectedName());
Intent i = new Intent(BoardInput.this, MainActivity.class);
startActivity(i);
}
}
});
}
这是BoardInput类创建的内容:
您在edittext中键入名称,然后单击确认将名称添加到列表视图并清除edittext。当您单击列表视图中的名称时,它将突出显示红色,并且该名称是我需要在MainActivity类中使用的名称。
答案 0 :(得分:1)
我在您的代码中进行了一些更改:
public class BoardInput extends Activity {
private EditText mUserInput;
private Button mConfirm;
private Button mReady;
private List<String> names = new ArrayList<>();
private ArrayList<Names> arrayOfNames = new ArrayList<Names>();
private CustomAdapter adapter;
private ListView listview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_boardinput);
adapter = new CustomAdapter(BoardInput.this, arrayOfNames);
listview = (ListView) findViewById(R.id.listview);
listview.setAdapter(adapter);
mUserInput = (EditText) findViewById(R.id.nameInput);
mConfirm = (Button) findViewById(R.id.confirm);
mConfirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mUserInput.getText().toString().equals("")) {
Toast.makeText(getApplicationContext(), "Please enter a name.", Toast.LENGTH_SHORT);
} else {
arrayOfNames.add(new Names(mUserInput.getText().toString()));
mUserInput.setText("");
**Edited**:
adapter.notifyDataSetChanged();
listview.invalidateView();
}
}
});
mReady = (Button) findViewById(R.id.ready);
mReady.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if (adapter.getSelectedName().isEmpty()){
Log.d("NULL","NULL");
}
else{
Log.d("BoardInput", adapter.getSelectedName());
Intent i = new Intent(BoardInput.this, MainActivity.class);
startActivity(i);
}
}
});
}
答案 1 :(得分:1)
当你想要MainActivity并且没有完成BoardInputActivity时,当MainActivity关闭时,ListView仍然存在。但是当你想要BoardInputActivity时,一切都将被重建,包括适配器。