我知道有关于notifyDataSetChanged()的在线信息;以及如何使用它但我似乎无法弄清楚如何在我的确切情况下使用它。我不知道是否应该在我的活动,适配器类或什么中调用它。我只是希望listView不会更改用户先前输入的文本,以便在行滚动屏幕时返回默认文本。
MainActivity.java
else {
$("#minus-btn").prop('disabled',true);
}
CustomAdapter.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewSwitcher;
import java.util.ArrayList;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ArrayList<String> Chores = new ArrayList<>();
Chores.add("");
final ListAdapter MyAdapter = new CustomAdapter(this, Chores);
ListView listViewObject = (ListView)findViewById(R.id.customListView_ID);
listViewObject.setAdapter(MyAdapter);
listViewObject.setOnItemClickListener(
new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
String ChoreString = String.valueOf(parent.getItemAtPosition(position));
}
}
);
final Button button = (Button) findViewById(R.id.button_ID);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Chores.add("");
((ArrayAdapter)MyAdapter).notifyDataSetChanged();
}
});
}
}
答案 0 :(得分:3)
问题不是由notifyDataSetChanged()引起的;但是因为您没有在适配器中重复使用视图而没有设置要在getView()中设置的数据
最好尝试使用viewholder模式或使用recyclerview。
class CustomAdapter extends ArrayAdapter{
ArrayList<String> choreText;
public CustomAdapter(Context context, ArrayList choreText) {
super(context, R.layout.custon_listview_row, choreText);
this.choreText = choreText;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
convertView = layoutInflater.inflate(R.layout.custon_listview_row, parent, false);
holder = new ViewHolder();
holder.imageButton = (ImageButton) customView.findViewById(R.id.imageButton_ID);
holder.textView = (TextView) customView.findViewById(textView_ID);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(choreText.get(position));
holder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//what happens when textView is clicked
AlertDialog alertDialog = new AlertDialog.Builder(getContext()).create();
final EditText input = new EditText(getContext());
input.setHint("hint");
alertDialog.setView(input);
alertDialog.setTitle("Set Chore");
alertDialog.setMessage("Alert message to be shown");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//this will set text to "hello" only if user does not enter anything into input
//textView.setText("hello");
//this also will only work if there is no input entered into input...wich will change textView into empty space
textView.setText(input.getText().toString());
//works the same with or without dialog.dismiss();
dialog.dismiss();
}
});
alertDialog.show();
}
});
holder.imageButton.setImageResource(R.drawable.clock);
return customView;
}
static class ViewHolder {
ImageButton imageButton;
TextView textView;
}
}
答案 1 :(得分:1)
Chores.add("");
。2.在滚动时,它会重新创建视图,以便更好地使用视图持有者。
3.要保留视图数据,请创建一个用于存储edittext文本数据的数组。
// initialise the array inside constructor
stringArray = new String[choreText.size()] ;
4.每次点击都会点击将数据保存到数组中并调用notifyDataSetChanged
alertDialog.setButton(AlertDialog.BUTTON_NE UTRAL, "OK", new DialogInterface.OnClickListener()
{ public void onClick(DialogInterface dialog, int which) {
//this will set text to "hello" only if user does not enter anything into input
textView.setText("hello");
//this also will only work if there is no input entered into input...which will change textView into empty space
textView.setText(input.getText().toString());
stringArray[position]=input.getText().toString();
notifyDataSetChanged();
//works the same with or without
dialog.dismiss(); dialog.dismiss(); } });
4.从数组中设置保留的文本,在getview中添加此行
textview.setText(stringArray[position]==null?"":stringArray[position];