问题
I have an EditText inside the ListView of 20 rows. I want the text of all the Edittext text on clicking of the button. The App gets crash giving the NullPointerException Error on clicking of the button.This gives value for first 11 rows.. after this it gets crashed as I think this is scrolled down view .
complete adapter code
@SuppressLint("NewApi") public class CustomAdapter extends BaseAdapter implements OnClickListener {
/*********** Declare Used Variables *********/
private Activity activity;
private ArrayList data;
private static LayoutInflater inflater=null;
public Resources res;
ListModel tempValues=null;
int i=0;
int j=0;
public ArrayList<String> selectedStrings = new ArrayList<String>();
/************* CustomAdapter Constructor *****************/
public CustomAdapter(Activity a, ArrayList d,Resources resLocal) {
/********** Take passed values **********/
activity = a;
data=d;
res = resLocal;
/*********** Layout inflator to call external xml layout () **********************/
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
/******** What is the size of Passed Arraylist Size ************/
public int getCount() {
if(data.size()<=0)
return 1;
return data.size();
}
public Object getItem(int position) {
return data.get(position);
}
public long getItemId(int position) {
return 0;
}
/********* Create a holder to contain inflated xml file elements ***********/
public static class ViewHolder{
public TextView text;
public TextView text1;
public EditText text2;
public TextView textWide;
public ImageView image;
public CheckBox check;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
final ViewHolder holder;
if(convertView==null){
/********** Inflate tabitem.xml file for each row ( Defined below ) ************/
vi = inflater.inflate(R.layout.tabitem, null);
/******** View Holder Object to contain tabitem.xml file elements ************/
holder=new ViewHolder();
holder.text=(TextView)vi.findViewById(R.id.textView1);
holder.check =(CheckBox)vi.findViewById(R.id.checkBox1);
holder.text2=(EditText)vi.findViewById(R.id.editText1);
holder.check.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v)
{
if (((CheckBox) v).isChecked())
{
holder.text2.setText("Checked");
}
else
{
holder.text2.setText("Not Checked");
}
}
});
vi.setTag(holder);
j++;
if(j<8){
holder.text2.setVisibility(View.INVISIBLE);
holder.check.setX(-150);
holder.check.setVisibility(View.VISIBLE);
}else
{
holder.check.setVisibility(View.INVISIBLE);
holder.text2.setVisibility(View.VISIBLE);
}
}
else
holder=(ViewHolder)vi.getTag();
holder.text2.getText();
if(data.size()<=0)
{
holder.text.setText("No Data");
holder.text2.getText();
}
else
{
/***** Get each Model object from Arraylist ********/
tempValues=null;
tempValues = (ListModel) data.get(position);
/************ Set Model values in Holder elements ***********/
holder.text.setText(tempValues.getCompanyName());
vi.setOnClickListener(this); }
return vi;
}
@Override
public int getViewTypeCount() {
return getCount();
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public void onClick(View v) {
Log.v("CustomAdapter", "=====Row button clicked");
TextView tv = (TextView)v.findViewById(R.id.textView1);
int pos = (Integer)tv.getTag();
Log.d("position of clicked item is", ""+pos);
}
/********* Called when Item click in ListView ************/
private class OnItemClickListener implements OnClickListener{
private int mPosition;
OnItemClickListener(int position){
mPosition = position;
}
@Override
public void onClick(View arg0) {
CustomListViewAndroidExample sct = (CustomListViewAndroidExample)activity;
sct.onItemClick(mPosition);
}
}
}
Button Click event which fails after 11 rows..
Button button =(Button)findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
for (int i=0;i< 20;i++ ){
View vListSortOrder;
vListSortOrder=list.getChildAt(i);
EditText edit=(EditText)vListSortOrder. findViewById(R.id.editText1);
String temp1223=edit.getText().toString();}
LOG
04-28 03:33:23.542: E/AndroidRuntime(4917): FATAL EXCEPTION: main
04-28 03:33:23.542: E/AndroidRuntime(4917): java.lang.NullPointerException
04-28 03:33:23.542: E/AndroidRuntime(4917): at com.androidexample.customlistview.CustomListViewAndroidExample$1.onClick(CustomListViewAndroidExample.java:89)
04-28 03:33:23.542: E/AndroidRuntime(4917): at android.view.View.performClick(View.java:4240)
04-28 03:33:23.542: E/AndroidRuntime(4917): at android.view.View$PerformClick.run(View.java:17721)
04-28 03:33:23.542: E/AndroidRuntime(4917): at android.os.Handler.handleCallback(Handler.java:730)
回答1:
use vListSortOrder=list.getAdapter().getView(i, null, null);
in stead of vListSortOrder=list.getChildAt(i);
Thanks
回答2:
you must store contents of editext and other information to show in listview in a model, and create arraylist of that model. This help you maintaing the list item state on scroll and also to get proper content.
And on Button click get the content from arraylist of that model...
来源:https://stackoverflow.com/questions/23337254/how-to-get-edittext-text-inside-listview