gpt4 book ai didi

android - 在列表 ListView 中单击按钮

转载 作者:行者123 更新时间:2023-11-30 02:17:51 25 4
gpt4 key购买 nike

我尝试了很多引用资料,但没有成功。我有一个带有 listview 的 simplecursoradapter。我在每个列表项中填充 textview 和一个按钮。我需要执行 2 个操作:1.按钮点击2. ListView 点击。使用 onListItemClick 我的代码运行完美。但我无法访问 ListView 中的按钮来执行(调用)。我的代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">



<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:layout_gravity="center_vertical"/>

<TextView
android:id="@+id/jobnumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:textStyle="bold"
android:textColor="#000000"
android:layout_marginRight="50dp"
android:height="30sp"
android:gravity="right" />

<TextView
android:id="@+id/complaintdate"
android:layout_width="257dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/jobnumber"
android:gravity="right" />

</RelativeLayout>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_horizontal"
>
<TextView
android:id="@+id/lbltenantname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:text="@string/Tenant">

</TextView>
<TextView
android:id="@+id/tenantname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal">

</TextView>

</LinearLayout>

和其他 TextView ....

list 来保存 Listitem...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/backgroundn"
android:descendantFocusability="blocksDescendants">

<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#000000"
android:dividerHeight="4dp"/>

</LinearLayout>

和我的安卓代码

import android.app.ActionBar;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class JobList extends ListActivity
{
SimpleCursorAdapter mAdapter;
ListView lv;
private DatabaseAdapter dbHelper;
Menu menuitem;



@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.joblist_item);

ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);

Intent intent=getIntent();
String BegeningUserid= intent.getStringExtra("USERID");
String SelfStatus= intent.getStringExtra("SELF");
Button call=(Button)findViewById(R.id.btn_call);


try
{

dbHelper=new DatabaseAdapter(this);
dbHelper.open();
lv=(ListView)findViewById(android.R.id.list);


Cursor c=dbHelper.fetchAllJobs(BegeningUserid,SelfStatus);

@SuppressWarnings("static-access")
String[] from = new String[] { dbHelper.COL_JOBNUMBER,dbHelper.COL_TENANT ,dbHelper.COL_DEVELOPMENT,dbHelper.COL_SHORTCODE,dbHelper.COL_PROPERTY,dbHelper.COL_COMPLAINTDATE,dbHelper.COL_COMPLAINTDESC,dbHelper.COL_MOBILE};
int[] to = new int[] {R.id.jobnumber, R.id.tenantname, R.id.location,R.id.ShortCode, R.id.Unit,R.id.complaintdate,R.id.complaintdesc,R.id.mobile};

@SuppressWarnings("deprecation")
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
JobList.this, R.layout.list_item, c, from, to);

adapter.notifyDataSetChanged();
lv.setAdapter(adapter);
if(lv.getCount()<=0)
{
Toast.makeText(getApplicationContext(), "NO JOBS ASSIGNED", Toast.LENGTH_LONG).show();
}




lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), "Clicked",Toast.LENGTH_SHORT).show();
}
});




}
catch(Exception e)
{
System.out.println(e);
}
finally
{

dbHelper.close();
}

我的 listitemclick 运行良好

public void onListItemClick(ListView parent, View view, int position, long id) 
{



}

我只需要单击 ListView 中的按钮并根据特定 ListView 位置的数据执行另一项 Activity 。

最佳答案

将此自定义适配器设置为您的 ListView ...

自定义适配器类

public class CustomAdapter extends BaseAdapter implements OnClickListener {

//song list and layout
private ArrayList<from> from;
private LayoutInflater songInf;

//constructor
public CustomAdapter(Context c, ArrayList<from> arrfrom){
from=arrfrom;
songInf=LayoutInflater.from(c);
}

@Override
public int getCount() {
return from.size();
}

@Override
public Object getItem(int arg0) {
return null;
}

@Override
public long getItemId(int arg0) {
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout songLay = (LinearLayout)songInf.inflate
(R.layout.list_item, parent, false);
TextView tv = (TextView)songLay.findViewById(R.id.from_text);
Button button = (Button)songLay.findViewById(R.id.from_button);

tv.setText(from.get(position));

button.setOnClickListner(this);

return songLay;
}

@Override
public void onClick(View v)
{

// Here is code after button click

}

关于android - 在列表 ListView 中单击按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29007166/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com