在android listview上添加按钮
我已经从本地数据库获取了数据,并将其显示在在android listview上添加按钮,android,android-listview,android-button,Android,Android Listview,Android Button,我已经从本地数据库获取了数据,并将其显示在列表视图中,我有一个带有按钮的自定义行 main.xml将仅包含ListView 自定义行.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/RelativeLayout1" style="@color/backgrou
列表视图中
,我有一个带有按钮的自定义行
main.xml将仅包含ListView
自定义行.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
style="@color/background_gradient_start"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:alpha="0.6"
android:background="@color/background_gradient_start"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="177dp"
android:layout_height="75dp"
android:layout_weight="5" >
<TextView
android:id="@+id/textViewFlightNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textViewCodes"
android:layout_alignBottom="@+id/textViewCodes"
android:layout_alignParentLeft="true" />
<TextView
android:id="@+id/textViewCodes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="18dp"
android:background="@color/transparent"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:textSize="16dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="142dp"
android:layout_height="match_parent"
android:layout_weight="5">
<Button
android:id="@+id/btn_share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:alpha="1"
android:background="#3b3974"
android:focusable="false"
android:focusableInTouchMode="false"
android:minWidth="90dp"
android:text="Share"
android:textColor="#f1ecef" />
</RelativeLayout>
</LinearLayout>
活动
所以在“创建”下
String [] fromFiledName =new String[]{db.KEY_CODE};
int[] toViewIDs=new int[]{R.id.textViewCodes};
SimpleCursorAdapter myCurAdapter=new SimpleCursorAdapter(
this, //context
R.layout.listview_row_codes,
cursor,
fromFiledName,
toViewIDs
);
// set addapter to list view
ListView mylistPRN=(ListView) findViewById(R.id.codeslist);
mylistPRN.setAdapter(myCurAdapter);
mylistPRN.setAdapter(myCurAdapter);
mylistPRN.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
//Intent intent2 = new Intent(this, PromoCodeActivity2.class);
Intent sendIntent = new Intent();
//intent.putExtra(PromoCodeActivity.requestString, httpRequestString);
//getItemAtPosition(position);
Cursor c = (Cursor) parent.getAdapter().getItem(position);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(Intent.EXTRA_TEXT, "share code "+c.getString(c.getColumnIndex("code")));
startActivity(Intent.createChooser(share, "Share Text"));
//startActivity(intentD);
}
});
String[]fromFiledName=新字符串[]{db.KEY\u code};
int[]toViewIDs=newint[]{R.id.textViewCodes};
SimpleCursorAdapter myCurAdapter=新的SimpleCursorAdapter(
这个,//上下文
R.layout.listview\u行代码,
光标,
fromFiledName,
toViewIDs
);
//将addapter设置为列表视图
ListView mylistPRN=(ListView)findViewById(R.id.codeslist);
mylistPRN.setAdapter(MyCuraAdapter);
mylistPRN.setAdapter(MyCuraAdapter);
mylistPRN.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父视图、视图、整型位置、,
长id){
//Intent intent2=新的Intent(这是PromotCodeActivity2.class);
Intent sendIntent=新Intent();
//putExtra(PromoCodeActivity.requestString、httpRequestString);
//getItemAtPosition(位置);
游标c=(游标)parent.getAdapter().getItem(位置);
意向共享=新意向(意向.行动\发送);
share.setType(“文本/普通”);
share.putExtra(Intent.EXTRA_TEXT,“共享代码”+c.getString(c.getColumnIndex(“代码”));
startActivity(Intent.createChooser(共享,“共享文本”));
//星触觉(意向);
}
});
代码正在工作,当我单击列表视图项目时,它将打开以供共享,但我需要在单击按钮时打开它,因为当前我尝试链接按钮,但我无法。任何帮助以下是您可以执行的操作将以下两行添加到按钮xml中
android:focusable="false"
android:focusableInTouchMode="false"
然后在适配器getView方法中,将onclickListener附加到此按钮,就完成了。当前行中的按钮没有附加侦听器。您不需要使用
onItemClickListner
,因为您没有单击列表项,而是必须在getview方法中设置按钮上的onClickListner
像这样的
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.imagelistlayout, null, true);
Button yourBtn = (Button)rowView.findViewById(R.id.your_btn_id);
yourBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//Do your work here
}
});
return rowView;
}
还可以在按钮上设置以下属性
android:focusable="false"
android:focusableInTouchMode="false"
每次我把这个方法放在onCreate之后,它都会显示错误,我必须删除@override,它看不到上下文。你不应该把它放在onCreate之后。您必须创建一个扩展BaseAdapter的自定义类,有关更多详细信息,请访问查看2。自定义ArrayAdapter示例