按钮单击列表ListView

时间:2015-03-12 10:23:26

标签: android

我尝试了很多参考,但没有让它工作。 我有一个带listview的simplecursoradapter。 我在每个listitem中填充textview和一个按钮。 我需要执行2个操作: 1.Button点击 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>

和其他textviews ....

列出举行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) 
 {



 }

我需要的只是列表视图中的单击按钮,并根据特定列表视图位置的数据执行另一个活动。

1 个答案:

答案 0 :(得分:1)

将此自定义适配器设置为列表视图...

自定义适配器类

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

}

}