【问题标题】:Android listview with multiple controls具有多个控件的 Android 列表视图
【发布时间】:2012-05-02 15:29:03
【问题描述】:

我是安卓新手。我正在开发一个应用程序,其中有一个带有编辑和删除按钮的学生列表视图。喜欢


 STUDENT LIST

[ABC]              [编辑]              [删除]

[DEF]              [编辑]              [删除]


使用此代码,我可以在<Textview> 中列出学生详细信息

public class DataBaseDemoActivity extends ListActivity {
/** Called when the activity is first created. */
  SQLiteDatabase db;
  Button btnInsert;
  ArrayAdapter<String> students;

  @Override
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  try{
  db=openOrCreateDatabase("StudentDB",SQLiteDatabase.CREATE_IF_NECESSARY,null);

  Cursor c=db.rawQuery("SELECT * FROM temp",null);
  String[] students = new String[c.getCount()];

  if (c.moveToFirst())
  {                       
      for (int i = 0; i < c.getCount(); i++)
      {
          students[i] = c.getString(0).toString()+" "+c.getString(1).toString();
          c.moveToNext();
      }           
  }
  c.close();

  setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, students));

  ListView lv = getListView();
  lv.setTextFilterEnabled(true);

  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(), ((TextView) view).getText(),
          Toast.LENGTH_SHORT).show();
    }
  });


  }catch(SQLException e)
  {
  }
}

}

我需要将记录的 ID 存储在列表视图中,这样当我单击编辑或删除按钮时,我必须找出 ID 并在数据库上进行更改。我如何将值设置为两个字段,例如 &lt;TextView&gt;[显示详细信息] 和 &lt;EditText&gt;[可见性:不可用 - 保存记录的 id]。我可以使用上面的代码获取&lt;TextView&gt; 的详细信息。任何建议将不胜感激。

更新:

我正在使用的布局

    <?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="horizontal"    
    >
    <EditText 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="invisible"
        android:inputType="text"
        android:onClick="showInfo"
        />
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="230dip"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:id="@+id/studentInfo" >
    </TextView>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/action"
        android:onClick="showInfo"
         />

</LinearLayout>

【问题讨论】:

  • "id of the record" 是指带有文本的数据库记录id?我对吗?
  • @user370305 : 我的意思是数据库记录的ID。

标签: java android xml listview


【解决方案1】:

您可以在游标数据获取循环中获取 Id,使用任何 HashMap&lt;id,String&gt; 集合并将其与数据(文本)一起存储。

根据您的要求,我建议您使用SimpleCursorAdapter 而不是ArrayAdapterListView

因此,您可以使用列表轻松管理数据库记录,并在单击列表项时获取与特定记录的数据库相关的所有信息。

SimpleCursorAdapters and ListViews

Creating a custom CursorAdapter for Android

【讨论】:

    【解决方案2】:

    在这一行之后..

       students[i] = c.getString(0).toString()+" "+c.getString(1).toString();
        student_ids[i]=Integer.parseInt(//get the  id... )
      //and you dont need to put it in invisible edittext...
    

    在 onclick 方法中,您将获得整数“位置”..并在该位置的 students_ids[] 中获得 tha 值..

     int id=students_ids[position];
    

    【讨论】:

    • 当我点击编辑按钮时我将如何获得整数位置?..你能告诉..
    • 你的按钮不是列表视图行的一部分吗?
    • 是按钮在行内。我已经更新了问题..添加了我的布局。请检查。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-06
    • 2011-07-14
    相关资源
    最近更新 更多