我新的Android應用程序的開發,得到的EditText值出現在列表視圖外,得到的EditText值內OnItemClick
我想其中存在的列表視圖外,我想進去OnItemClick值的EditText值。當用戶輸入文本並單擊列表項目時,我需要同時獲取列表項目和edittext文本值。
我創建含有一個的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>
U可以採取setOnItemClickListener在MainActivity –
感謝您的回覆茶匙。我嘗試過,但我需要這樣的方式......如果不行,我會改變我的方案到你的指導方針。再次感謝茶匙。 –