我在ListView中有一个20行的EditText。我想要点击按钮时所有Edittext文本的文本。应用程序崩溃,在单击按钮时出现NullPointerException错误。这为前11行提供了值..之后它会崩溃,因为我认为这是向下滚动的视图。
完整的适配器代码
@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);
}
}
}
按钮点击事件 在11行之后失败..
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)
答案 0 :(得分:3)
使用vListSortOrder = list.getAdapter()。getView(i,null,null);
代替vListSortOrder = list.getChildAt(i);
由于
答案 1 :(得分:0)
您必须存储editext和其他信息的内容以在模型中的listview中显示,并创建该模型的arraylist。这有助于您在滚动时维护列表项状态,并获得适当的内容。
点击按钮,点击该模型的arraylist中的内容......