0
我在寫一個Android程序。我的程序有一個自定義的ListView。 現在列表中的每一行都包含一個TextField和一個Button。 我希望Textfield在我按下按鈕時作出反應。給一個按鈕一個動態標籤/ android
硬編碼時這很簡單,但是如何動態地執行此操作?
對我來說,爲此我應該能夠爲每個按鈕和文本字段動態分配一個標籤,以便每個按鈕都具有與其旁邊的文本字段相同的標籤。
編輯: 我不認爲它會幫助任何,但這裏是我迄今爲止嘗試,首先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"
/>
對於java代碼:
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();
}
}
正如你所看到的,我有一個帶有動態生成按鈕的列表視圖 - 現在我如何解決他們中的一個,比如說第四或第五或第十六個?
一定是有可能性的,但它是該死很難找到
發表您已經嘗試過的內容 –
我仍然對如何訪問listviews組件問題感到困惑。我當然可以發佈我的XML文件,也是我的視圖,但是我還沒有到目前爲止嘗試訪問列表視圖中的按鈕,如果我知道如何做,我不會問這裏。 – user1862770