我是Android应用开发的新手,
我想获得在listview之外出现的edittext值,我想在OnItemClick中获取值。当用户输入文本并单击列表项时,我需要同时选择列表项和编辑文本文本值。
我创建了包含一个edittext和listview的主布局。我想在listview OnItemClick事件中获取edittext值。
我试过我无法得到它。请指导我。
这里我列出了我的示例程序。
MainActivity.java
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends Activity {
ArrayList<String> listVal = new ArrayList<String>();
public EditText searchKey;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
searchKey = (EditText) findViewById(R.id.searchKey);
ListView spinner = (ListView) findViewById(R.id.listView1);
listVal.add("Apple");
listVal.add("Avocado");
listVal.add("Banana");
listVal.add("Blueberry");
listVal.add("Coconut");
listVal.add("Durian");
listVal.add("Guava");
listVal.add("Kiwifruit");
listVal.add("Jackfruit");
ListAdapter listAdapter = new ListAdapter(getApplicationContext(), R.layout.list, listVal);
spinner.setAdapter(listAdapter);
spinner.setOnItemClickListener(listAdapter);
}
}
ListAdapter.java
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class ListAdapter extends ArrayAdapter<String> implements OnItemClickListener {
private final Context context;
private final ArrayList<String> values;
int layoutResourceId;
public ListAdapter(Context context, int layoutResourceId, ArrayList<String> values) {
super(context, layoutResourceId, values);
this.context = context;
this.layoutResourceId = layoutResourceId;
this.values = values;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(layoutResourceId, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.listItem);
textView.setText(values.get(position));
return rowView;
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
TextView message = (TextView)arg1.findViewById(R.id.listItem);
Toast.makeText(context, message.getText().toString(), Toast.LENGTH_SHORT).show();
}
}
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/searchKey"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:ems="10" >
<requestFocus />
</EditText>
<ListView
android:id="@+id/listView1"
android:layout_below="@+id/searchKey"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" >
</ListView>
</RelativeLayout>
list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/listItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dip"
android:textColor="#000000" />
</LinearLayout>
答案 0 :(得分:1)
你也可以使用它,它可以帮助其他人..
spinner.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View v, int pos,
long id) {
// TODO Auto-generated method stub
int p = values.get(pos);
String key = searchKey.getText().toString().trim();
Log.v("listitem_value",p);
Log.v("EditTextValue",key);
Toast.makeText(context, "listitem_value:"+p+"\nEditTextValue:"+key, Toast.LENGTH_SHORT).show();
}
});
答案 1 :(得分:0)
试试这个..
public class MainActivity extends Activity {
ArrayList<String> listVal = new ArrayList<String>();
public EditText searchKey;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
searchKey = (EditText) findViewById(R.id.searchKey);
ListView spinner = (ListView) findViewById(R.id.listView1);
listVal.add("Apple");
listVal.add("Avocado");
listVal.add("Banana");
listVal.add("Blueberry");
listVal.add("Coconut");
listVal.add("Durian");
listVal.add("Guava");
listVal.add("Kiwifruit");
listVal.add("Jackfruit");
ListAdapter listAdapter = new ListAdapter(getApplicationContext(), R.layout.list, listVal);
spinner.setAdapter(listAdapter);
spinner.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View v, int pos,
long id) {
// TODO Auto-generated method stub
Log.v("listitem_value",values.get(pos));
Log.v("EditTextValue",searchKey.getText().toString().trim());
Toast.makeText(context, "listitem_value:"+values.get(pos)+"\nEditTextValue:"+searchKey.getText().toString().trim(), Toast.LENGTH_SHORT).show();
}
});
}
}
并删除implements OnItemClickListener
onItemClick
和ListAdapter
方法
答案 2 :(得分:0)
spinner.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View v, int pos,
long id) {
// TODO Auto-generated method stub
TextView message = (TextView)arg1.findViewById(R.id.listItem);
Toast.makeText(context, message.getText().toString(), Toast.LENGTH_SHORT).show();
Log.v("EditText",searchKey.getText().toString().trim());
}
});