1

i tried many reference to this,but didn't get it working. I have a simplecursoradapter with listview . I populate textview and a button in each listitem. I need to perform 2 operations: 1.Button Click 2.Listview Click. Using onListItemClick my code is running perfect.but i cannot access the button inide the listview to perform(a call). My code below:

<?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>

and other textviews....

list to hold the 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>

and my android code

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();
}

}

my listitemclick which is working perfect

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



 }

all i need is click button inside the listview and perform another activity depending on the data from the particular listview position.

Blair Pereira
  • 27
  • 1
  • 10
  • create a custom adapter and get the button in the adapter and add onClickListener there in the adapter it will work. – Akshay Paliwal Mar 12 '15 at 10:25
  • Yes try custom adapter – Sunil Kumar Mar 12 '15 at 10:30
  • Can i handle this in the same class,using the cursor to fetch data and assign accordingly?I have to create array adapter and stuff? – Blair Pereira Mar 12 '15 at 10:40
  • Can i get some help in creating custom adapter based on my code. – Blair Pereira Mar 12 '15 at 11:02
  • using custom adapter,i cannot fetch data from sqlite because my listview is populated based on condition from intent:Intent intent=getIntent(); String BegeningUserid= intent.getStringExtra("USERID"); String SelfStatus= intent.getStringExtra("SELF"); from my above code – Blair Pereira Mar 12 '15 at 11:52

1 Answers1

1

set this custom Adapter to your list view...

Custom Adapter Class

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

}

}

Bhaskar
  • 911
  • 6
  • 18
  • i added this class in class JobList,but im still confused with getting data from sqlite with my cursor and assigning to custom adapter... – Blair Pereira Mar 12 '15 at 14:07
  • In my code above i set my listview base on data from cursor.how do i go about it? with using custom adapter? – Blair Pereira Mar 12 '15 at 14:55