我想在运行时使用EditText中的数据填充自定义列表视图。我怎么能得到它? .................................................. .................................................. .................................................. ............... 到目前为止我的代码:
package com.example.additemlistrun;
import java.util.ArrayList;
import android.app.ListActivity;
public class MainListActivity extends ListActivity {
public ArrayList<String> list = new ArrayList<String>();
static String[] mTilte;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_list);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override``
public void onClick(View arg0) {
EditText et = (EditText) findViewById(R.id.editText1);
String gettingdata = et.getText().toString();
// Unable to pass data from editext to MyAdapter class
mTilte[0] = gettingdata;
MyAdapter adapter = new MyAdapter(MainListActivity.this, mTilte);
setListAdapter(adapter);
}
});
}
}
class MyAdapter extends ArrayAdapter<String> {
Context context;
String[] ArrayTitle;
MyAdapter(Context c, String[] title) {
super(c, R.layout.single_row, title);
this.context = c;
this.ArrayTitle = title; }
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.single_row, parent, false);
TextView titles = (TextView) row.findViewById(R.id.textView1);
titles.setText(ArrayTitle[position]);
return row;
}
}
Logcat错误::
答案 0 :(得分:1)
你的空指针可能在这里发生:
EditText et = (EditText) findViewById(R.id.editText1);
找不到视图 - 您需要将EditText(和ListView)声明为Activity变量,并在分配onClickListener之前分配它们。试试这个:
public class MainListActivity extends ListActivity {
public ArrayList<String> list = new ArrayList<String>();
static String[] mTilte;
EditText et;
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_list);
Button b = (Button) findViewById(R.id.button1);
et = (EditText) findViewById(R.id.editText1);
mTilte = new String[]{};
list = getListView();
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String gettingdata = et.getText().toString();
mTilte[0] = gettingdata;
MyAdapter adapter = new MyAdapter(MainListActivity.this, mTilte);
list.setListAdapter(adapter);
}
});
}
}