Failed to get row number with pressed number in Android list view

I am trying to create a simple sms application for which the edit page contains a list of all saved sms. I gave two buttons in my layout for “Edit” and “Delete”, but when I click on them, I can’t get the rowid (line number above) to get the sms corresponding to this particular line. Someone please help me .. my sms adapter does not apply to anything .. Here are my class and xml files ..

public class SMSEdit extends ListActivity{ private SMSDbAdapter mDbHelper; //private Button editsmsbtn, deletesmsbtn; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.editsmslist); mDbHelper = new SMSDbAdapter(this); mDbHelper.open(); fillData(); } public void fillData() { Cursor smsCursor = mDbHelper.fetchAllNotes(); startManagingCursor(smsCursor); // Create an array to specify the fields we want to display in the list (only TITLE) String[] from = new String[]{SMSDbAdapter.KEY_TITLE, SMSDbAdapter.KEY_BODY, SMSDbAdapter.KEY_DATE, SMSDbAdapter.KEY_TIME}; // and an array of the fields we want to bind those fields to (in this case just text1) int[] to = new int[]{R.id.smseditlistTO, R.id.smseditlistMessage, R.id.smseditlistDay, R.id.smseditlistTime}; // Now create a simple cursor adapter and set it to display SimpleCursorAdapter smsadap = new SimpleCursorAdapter(this, R.layout.smsrow, smsCursor, from, to); setListAdapter(smsadap); } public void myEditClickHandler(View v) { //get the row the clicked button is in LinearLayout vwParentRow = (LinearLayout)v.getParent(); Button btnChild = (Button)vwParentRow.getChildAt(4); btnChild.setText("I've been clicked!"); ListAdapter pos = getListAdapter(); Intent intent = new Intent(); intent.setClass(SMSEdit.this, SMS.class); // intent.putExtra(SMSDbAdapter.KEY_ROWID, id); startActivity(intent); finish(); } public void myDeleteClickHandler(View v) { //get the row the clicked button is in LinearLayout vwParentRow = (LinearLayout)v.getParent(); //TextView child = (TextView)vwParentRow.getChildAt(0); Button btnChild = (Button)vwParentRow.getChildAt(5); //btnChild.setText(child.getText()); btnChild.setText("I've been clicked!"); int c = Color.CYAN; vwParentRow.setBackgroundColor(c); vwParentRow.refreshDrawableState(); } } 

editsmslist.xml →>

  <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ListView android:id="@+id/android:list" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/android:empty" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="No Future SMS Yet" android:textSize="35px"/> </LinearLayout> 

smsrow.xml →>

  <?xml version="1.0" encoding="utf-8"?> <ScrollView android:id="@+id/ScrollView01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <LinearLayout android:id="@+id/LinearLayout02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <LinearLayout android:id="@+id/LinearLayout03" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/smsdetailseditlist" android:id="@+id/smseditlistTO" android:paddingLeft="5px"> </TextView> <TextView android:layout_height="wrap_content" android:text="@string/smsdetailseditlist" android:id="@+id/smseditlistMessage" android:paddingLeft="20px" android:maxLines="1" android:layout_width="wrap_content"> </TextView> </LinearLayout> <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingLeft="5px"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Date: "></TextView><TextView android:text="@string/smsdetailseditlist" android:id="@+id/smseditlistDay" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Time: " android:paddingLeft="5px"> </TextView><TextView android:text="@string/smsdetailseditlist" android:id="@+id/smseditlistTime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingRight="10px"></TextView> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/EditSMSButton" android:text="@string/editsms" android:onClick="@string/myEditClickHandler"></Button> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/DeleteSMSButton" android:text="@string/deletesms" android:onClick="@string/myDeleteClickHandler"></Button> </LinearLayout> </LinearLayout> </ScrollView> 

Also, please suggest if you have the best way to implement this or make the layout look better ...

+4
source share
3 answers

You can expand cursorAdapter like this ...

 public class exampleAdapter extends CursorAdapter { LayoutInflater inflater; public exampleAdapter (Context context, Cursor c) { super(context, c); inflater = LayoutInflater.from(context); } @Override public void bindView(View view, Context context, Cursor cursor) { // TODO Auto-generated method stub Button btn_del = (Button)view.findViewById(R.id.button1); btn_del.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub SMSDbAdapter vb = new SMSDbAdapter(context); vb.open(); String xyz = cursor.getString(0); //Persumably your ID vb.Delete(Integer.parseInt(cursor.getString(0))); Toast.makeText(context, xyz + " Deleted!", Toast.LENGTH_SHORT).show(); vb.close(); } }); } @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { return inflater.inflate(R.layout.smsrow, parent, false); } } 
+1
source

I had a similar problem with my ListView .

Suppose you dynamically populate a ListView . You have 5 lines (for example). The identifier of the first line is 1 second - 2, etc.

When you look at all the lines visible to you, the identifier of the first line (which you see on the screen) will always be 1. You will need to manage this using mabye another class ( XYZ ).

I just used an ArrayList<XYZ> and it worked.

You can override this method in SMSAdapter :

 public View getView(int position, View convertView, ViewGroup parent) 

and then call the get(position) method, which returns an ArrayList<XYZ> .

0
source

You must use ListView to display your posts.

Store the data received from the database in an array of strings, such as s1. Then

private ArrayList<String> arr_sort = new ArrayList<String>(); And copy all s1 to arr_sort using the add () method.

When setting up the adapter

lv.setAdapter(new ArrayAdapter<String>(DataAttach.this, android.R.layout.simple_list_item_1, arr_sort)); where lv is the ListView link.

Then use this method for onClick

 lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String item = (String) lv.getItemAtPosition(position); Toast.makeText(*.this, "You selected - " + item + " ,Please wait...", Toast.LENGTH_LONG).show(); Intent intent = new Intent(*.this, *.class); *.this.startActivity(intent); } }); 

Hope this helps.

0
source

Source: https://habr.com/ru/post/1333759/


All Articles