Here is my sample code for editing each row, as well as the list database. Hope this helps you.
First create the adapter name "ListAdapterItem.java"
import java.util.ArrayList; import java.util.HashMap; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.RelativeLayout; import android.widget.TextView; public class ListAdapterItem extends BaseAdapter { private ArrayList<HashMap<String, Object>> list; private Context context; private RelativeLayout ll;
And here is my data_list_item
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="fill_parent" android:orientation="vertical" android:padding="5dp"> <TextView android:id="@+id/txtview_id" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_margin="8dp" android:layout_marginRight="2dp" android:textColor="@android:color/black" android:textSize="22dp" android:visibility="invisible"/> <TextView android:id="@+id/txtview_country_name" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_margin="8dp" android:layout_marginRight="2dp" android:textColor="@android:color/black" android:textSize="22dp" /> </RelativeLayout>
And the main class is "Main.java"
public class Main extends Activity implements OnClickListener { String name; SQLiteDatabase db; Cursor cursor; private ProgressDialog progressDialog; public ListAdapterItem list_adapter; private ArrayList<HashMap<String, Object>> lst_data; private HashMap<String, Object> hm; private ListView listview; private String list_id; private int counter_id,selectedPosition; private View selectedView; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_layout); lst_data = new ArrayList<HashMap<String, Object>>(); listview = (ListView) findViewById(R.id.list); new FetchDB().execute(); } private class FetchDB extends AsyncTask<String, Void,ArrayList<HashMap<String, Object>>> { protected void onPreExecute() { progressDialog = ProgressDialog.show(Main.this,"Fetching Data", "Loading,Please wait...", true); } protected ArrayList<HashMap<String, Object>> doInBackground(String... lstStrings)throws IllegalArgumentException { try { db = openOrCreateDatabase("MyDB", MODE_PRIVATE, null); cursor = db.rawQuery("SELECT * FROM person WHERE Id <> ''",null); if (cursor != null && cursor.getCount() > 0) { if (cursor.moveToFirst()) { do { hm = new HashMap<String, Object>(); hm.put("Id",cursor.getInt(cursor.getColumnIndex("Id"))); hm.put("Name", cursor.getString(cursor.getColumnIndex("Name"))); lst_data.add(hm); } while (cursor.moveToNext()); }
And main_layout.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:background="@android:color/transparent" android:cacheColorHint="#00000000" android:dividerHeight="2dp" android:paddingTop="8dp" android:transcriptMode="normal" /> </LinearLayout>
Indicate your identifier for each list in the list, hoping this answer to your question
Cjames
source share