我有一个带有按钮的自定义ListView,当我点击任何一行上的任一按钮时,我想在Listview上获取文本标签,现在只需弹出一个祝酒词。
我有list_item.xml:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow android:layout_width="fill_parent"
android:id="@+id/TableRow_list_item"
android:layout_height="wrap_content"
android:gravity="center">
<ImageView
android:layout_width="0dp"
android:layout_height="80dp"
android:id="@+id/imageView_logo_utente"
android:src="@drawable/icon_user_green"
android:layout_column="1"
android:layout_weight="1"/>
<TextView
android:textColor="#000000"
android:id="@+id/nomec"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Nome Completo"
android:layout_weight="2"
android:gravity="center"
android:height="40dp"
android:textSize="17dp"
android:layout_column="1"/>
<TextView
android:textColor="#000000"
android:id="@+id/tiposogget"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Tipo Soggetto Etichetta"
android:layout_weight="2"
android:gravity="center"
android:height="40dp"
android:textSize="17dp"
android:layout_column="1"/>
<TextView
android:textColor="#000000"
android:id="@+id/cf"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Codice Fiscale"
android:gravity="center"
android:height="40dp"
android:textSize="17dp"
android:layout_column="1"
android:layout_weight="2" />
<TextView
android:textColor="#000000"
android:id="@+id/idsogg"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="ID Soggetto"
android:layout_weight="1"
android:gravity="center"
android:height="40dp"
android:textSize="17dp"
android:layout_column="1"/>
<Button
android:layout_width="0dp"
android:layout_height="90dp"
android:id="@+id/button_scan"
android:layout_column="1"
android:layout_weight="0.63"
android:height="140dp"
android:background="@drawable/custom_button_scanner"
android:onClick="myClickHandler"
android:focusable="false"/>
</TableRow>
按钮 “android:onClick =”myClickHandler“”..
在我的活动中,我有这个:
公共类ActivityListview扩展了AppCompatActivity {
//************VARIABILI GLOBALI*************
ListView lv;
static final String KEY_CF = "CodiceFiscale";
static final String KEY_ID = "IDSoggetto";
static final String KEY_NOMEC = "NomeCompleto";
static final String KEY_TIPOSOGGET = "TipoSoggettoEtichetta";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_scanner);
lv = (ListView) findViewById(R.id.listView_resultXML);
Example();
}
public void Example() {
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
// Adding menuItems to ListView
ListAdapter adapter = new SimpleAdapter(this, menuItems,
R.layout.list_item,
new String[]{KEY_CF, KEY_ID, KEY_NOMEC, KEY_TIPOSOGGET}, new int[]{
R.id.cf, R.id.idsogg, R.id.nomec, R.id.tiposogget});
// selecting single ListView item
ViewGroup headerview = (ViewGroup) getLayoutInflater().inflate(R.layout.header_listview, lv, false);
lv.setAdapter(null);
lv.addHeaderView(headerview);
lv.setAdapter(adapter);
public void myClickHandler(View v)
{
// HERE I WOULD LIKE TO get vaule as method setOnItemClickListener
Toast.makeText(ActivityListviewScanner.this, "value is:", Toast.LENGTH_LONG).show();
}
方法:
// listening to single listitem click
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//Metodo per prelevare dati al click sulla casella della ListView
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String string_example = ((TextView) view.findViewById(R.id.cf)).getText().toString();
}
});
}
}
我想在onclick按钮中执行..
我该怎么办?
谢谢大家,对不起我的英文
答案 0 :(得分:0)
您需要从SimpleAdapter扩展,这样您就可以在监听器中保存对textview的引用,您可以在适配器的getView方法中声明它。
如果您在创建适配器时需要帮助,请查看以下其他问题: Custom Adapter for List View
答案 1 :(得分:0)
创建一个扩展BaseAdapter的类。并在getview方法中注册你的按钮并设置onClick函数。
public class Holder {
TextView tv1;
TextView tv2;
TextView tv3;
TextView tv4;
Button bt;
ImageView img;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder = new Holder();
View rowView;
rowView = inflater.inflate(R.layout.list_item, null);
holder.tv1 = (TextView) rowView.findViewById(R.id.nomec);
holder.tv2 = (TextView) rowView.findViewById(R.id.tiposogget);
holder.tv3 = (TextView) rowView.findViewById(R.id.cf);
holder.tv4 = (TextView) rowView.findViewById(R.id.idsogg);
holder.img = (ImageView) rowView.findViewById(R.id.imageView_logo_utente);
holder.bt = (Button) rowView.findViewById(R.id.button_scan);
holder.bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//write your code here
}
});
return rowView;
}