I am trying to get value from EditText in a listView .How to get value from EditText in a listView??I'm new in android programming, someone can help me?
i have a structure like this in my ListView
TextView EditText EditText
TextView EditText EditText
TextView EditText EditText
MainActivity
public class MainActivity extends Activity {
ListView list;
private myAdapter adapter;
private List<member>member_list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView)findViewById(R.id.list);
set_list();
}
private void set_list()
{
member_list = new ArrayList<>();
member_list.add(new member("ABC","1","1"));
member_list.add(new member("ABC","2","1"));
member_list.add(new member("ABC","1","2"));
member_list.add(new member("ABC","0","0"));
member_list.add(new member("ABC","2","2"));
member_list.add(new member("ABC","1","0"));
adapter = new myAdapter(getApplicationContext(),member_list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(),position,Toast.LENGTH_SHORT).show();
}
});
}
myAdapter
public class myAdapter extends BaseAdapter {
private List<member>member_list ;
private Context context;
public myAdapter(Context context,List<member> member_list)
{
this.context = context;
this.member_list = member_list;
}
@Override
public int getCount() {
return member_list.size();
}
@Override
public Object getItem(int position) {
return member_list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = View.inflate(context,R.layout.textview,null);
TextView t_name;
EditText t_text1,t_text2;
t_name = (TextView)v.findViewById(R.id.t_name);
t_text1 = (EditText)v.findViewById(R.id.t_text1);
t_text2 = (EditText)v.findViewById(R.id.t_text2);
t_name.setText(member_list.get(position).getName());
t_text1.setText(member_list.get(position).getNum1());
t_text2.setText(member_list.get(position).getNum2());
v.setTag(member_list.get(position));
return v;
}
答案 0 :(得分:0)
如果您想在点击时从 EditText 获取文字。您应该将监听器添加到 EditText ,然后让 EditText 接受点击事件,而不是列表。
起初:
android:clickable="true" android:focusable="true"
您可以在代码洞察中找到类似的设置功能onClickListener
设置为您的EditText并致电edittext.getText().toString()
即可。希望这会对你有所帮助
答案 1 :(得分:0)
v.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Log.v("OpenDbListAdapter ","List View Clicked");
String txt2 = t_text1.getText().toString();
Log.i("t_text1", "" + txt2);
}
});
在适配器中添加此项,并确保已将Textview和Edittext值全局定义。感谢
答案 2 :(得分:0)
要访问MainActivity中的EditText值,请更新getView方法,如下所示:
@Override
//Make sure you set position as final here
public View getView(final int position, View convertView, ViewGroup parent) {
....
EditText t_text1,t_text2;
.....
t_text1.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
((MainActivity)context).textChanged(position, charSequence);
}
@Override
public void afterTextChanged(Editable editable) {
}
});
return v;
}
.....
然后在MainActivity中创建方法textChanged()
一般来说,将context
转换为特定活动并不是一种很好的做法,因为它会使在其他地方重用此适配器变得更加困难,但它应该适用于您的目的。