2015-03-12 6 views
1

Я пробовал много ссылок на это, но не получил его работу. У меня есть simplecursoradapter с listview. Я заполняю текстовое изображение и кнопку в каждом списке. Мне нужно выполнить 2 операции: 1.Button Click 2.Listview Click. Использование onListItemНажмите, что мой код работает perfect.but, но я не могу получить доступ к кнопке внутри списка, чтобы выполнить (вызов). Мой код ниже:Кнопка Щелкните внутри списка 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) 
{ 



} 

все что мне нужно, это нажать кнопку внутри ListView и выполнить другое действие в зависимости от данных от конкретной позиции ListView.

+0

создания пользовательского адаптера и получить кнопку адаптера и добавить onClickListener там в адаптер будет работать. –

+0

Да попробовать пользовательский адаптер –

+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 

} 

}

+0

я добавил этот класс в класс JobList, но им все еще путают с получением данных из sqlite с помощью курсора и назначением пользовательского адаптера ... –

+0

В моем коде выше я установил базу данных списка по данным из cursor.how do я об этом? с использованием пользовательского адаптера? –