我正在编写一个Android程序。我的程序有一个自定义的ListView。 现在列表的每一行都包含一个TextField和一个Button。 当按下按钮时,我希望Textfield做出反应。
硬编码时这很简单,但我该如何动态执行此操作?
对我来说,为此我应该能够动态地为每个按钮和文本字段分配一个标签,这样每个按钮都有与它旁边的文本字段相同的标签。
编辑: 我认为它不会对任何人有所帮助,但是到目前为止我已经尝试过了,首先是xml文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res /android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.pris.ViewActivity"
tools:ignore="MergeRootFrame">
<TextView
android:id="@+id/tex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#0000ff"
android:text="@string/myText" />
<AutoCompleteTextView
android:id="@+id/textfield"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:layout_below="@+id/tex"
android:completionThreshold="1"
android:inputType="text" />
<Button
android:id="@+id/but"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/suchen"
android:layout_below="@id/tex"
android:layout_toRightOf="@+id/textfield"
android:onClick="addToList"
android:layout_alignParentRight="true"
/>
<Button
android:id="@+id/but2"
android:layout_weight="1.0"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Liste löschen"
android:layout_below="@+id/but"
android:layout_alignParentRight="true"
android:onClick="removeList" />
<ListView
android:id="@android:id/list"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1.0"
android:choiceMode="singleChoice"
android:layout_below="@+id/but2"></ListView>
和
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/Itemname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:paddingTop="5dp"/>
<Button
android:id="@+id/out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:onClick="rem"
/>
至于javacode:
package com.example.pris;
//HauptView
public class ViewActivity extends ListActivity {
//Wirkstoffliste. Nötig für den Adapter fürs Autocomplete
private static final String[] MEDICATIONS = {"Indometacin", "Acemetacin", "Ketoprofen", "Phenylbutazon",
"Piroxicam", "Meloxicam", "Etoricoxib", "Pethidin", "Chinidin", "Flecainid", "Sotalol", "Digoxin",
"Nitrofurantoin", "Hydroxyzin", "Clemastin", "Dimetinden", "Chlorphenamin"
};
//Adapter für die ExpandableListView
private ArrayAdapter adap;
//Textfeld, in das der User seine Anfrage eingibt
private AutoCompleteTextView edit;
//der aktuelle Context
final Context con = this;
public int selectedItem = -1;
protected Object mActionMode;
private String chosen;
private List<String> meds = new ArrayList();
//Das passiert beim Betätigen des Searchbuttons:
//Es wird ein Asynctask erstellt, der abfragt, ob der Wirkstoff in der Liste vorkommt und dann entsprechend die Anzeige modelliert.
//Danach wird das Textfeld wieder auf Null gesetzt.
public void addToList(View view) {
System.out.println(view);
String str = "";
boolean here = false;
str = edit.getText().toString();
for (int i = 0; i < MEDICATIONS.length; i++)
if (MEDICATIONS[i].equals(str)) {
here = true;
boolean duplicate = false;
for (int j = 0; j < meds.size(); j++) {
if (meds.get(j).equals(str)) {
duplicate = true;
break;
}
}
if (!duplicate) {
// adap.add(str);
meds.add(str);
adap.notifyDataSetChanged();
View v = this.getListView();
System.out.println(v);
break;
}
if (!here){
Toast.makeText(ViewActivity.this, "Bitte geben Sie einen anderen Wirkstoff ein", Toast.LENGTH_SHORT).show();
}
edit.setText(null);
}}
public void rem(View view){
}
//Entfernt bei Anklicken von "Liste löschen" die gesamte Liste
public void removeList(View view) {
meds.clear();
adap.clear();
adap.notifyDataSetChanged();
}
@Override
//OnCreate. Die View wird ans xml gebunden und weitere Methoden ausgelöst.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
adap = new ArrayAdapter<String>(
this, R.layout.mylist,
R.id.Itemname, meds);
this.setListAdapter(adap);
// adap = new ArrayAdapter<String>(ViewActivity.this, android.R.layout.simple_list_item_activated_1);
if (getIntent().getExtras()!= null){
System.out.println("not null");
Bundle bundle = getIntent().getExtras();
int i;
String str;
for (i = 0; i< bundle.size(); i++){
System.out.println(i);
System.out.println(bundle);
System.out.println(bundle.keySet());
str = bundle.getString(String.valueOf(i));
meds.add(str);
adap.add(str);
}
/* if (i==0){
meds.clear();
adap.clear();
}
*/}
else
{System.out.println(" null");}
setAutocomplete();
//setExpandList();
}
}
正如你所看到的,我有一个动态生成按钮的Listview - 现在如何解决其中一个,比如第四个或第五个或第16个?
必须有一些可能性,但很难找到
答案 0 :(得分:1)
以下是我从tutorial获取的一个例子。 看看称为
的方法适配器中的getView()
。
public class MainActivity extends Activity {
ListView listView;
MyThumbnaildapter thadapter=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
listView = (ListView)findViewById(R.id.list_fruit);
String[] name=new String[]{"fruit_logo1","fruit_logo2","fruit_logo3","fruit_logo4","fruit_logo5","fruit_logo6","fruit_logo7","fruit_logo8","fruit_logo9","fruit_logo10","fruit_logo11","fruit_logo12"};
String[] pic=new String[]{"Apple","Banana","Papaya","Coconut","Pineapple","Grapes","Guava","Tomato","Watermelon","Lime","Orange","Mango"};
thadapter=new MyThumbnaildapter(MainActivity.this,R.layout.list,pic,name);
listView.setAdapter(thadapter);
thadapter.notifyDataSetChanged();
}
public class MyThumbnaildapter extends ArrayAdapter<String> {
private String[] ar;
private String[] pic;
public MyThumbnaildapter(Context context, int textViewResourceId, String[] ar,String[] pic) {
super(context, textViewResourceId, ar);
this.ar=ar;
this.pic=pic;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if(view==null)
{
LayoutInflater inflater=getLayoutInflater();
view=inflater.inflate(R.layout.list, parent, false);
TextView textView=(TextView)view.findViewById(R.id.text_hint);
ImageView imageView=(ImageView)view.findViewById(R.id.img);
view.setTag(new Holder(imageView,textView));
}
Holder h = (Holder) view.getTag();
h.textView.setText(ar[position]);
int resID = getResources().getIdentifier(pic[position], "drawable", getPackageName());
h.imageView.setImageResource(resID);
return view;
}
}
private class Holder{
public final ImageView imageView;
public final TextView textView;
private Holder(ImageView imageView,TextView textView) {
this.imageView = imageView;
this.textView = textView;
}
}
}