简体   繁体   English

如何从列表视图的每一行中获取项目?

[英]How do I get the item from each row of a listview?

I wanna get the data in the clicked row in listView. 我想在listView中单击的行中获取数据。

This is my code , I want to access to "ID" Value in each row od list View. 这是我的代码,我想在列表视图的每一行中访问“ ID”值。

How do I get the item from each row of a listview? 如何从列表视图的每一行中获取项目?

How do I get the item from each row of a listview? 如何从列表视图的每一行中获取项目?

public class ActivityBookMark extends Activity {

private SQLiteDatabase database;
private ListView lv;
private int ID;
public ArrayAdapter<String> adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.book_mark);
    lv = (ListView) findViewById(R.id.BookMarkListView);
    Animation animation = AnimationUtils.loadAnimation(this,
            R.anim.translate);
    lv.setLayoutAnimation(new LayoutAnimationController(animation));
    ArrayList<String> data = geData();
    adapter = new ArrayAdapter<String>(this, R.layout.bookmark_item,
            R.id.TXTbookmarkItem, data);
    lv.setAdapter(adapter);
}

public ArrayList<String> geData() {
    // TODO Auto-generated method stub
    database = SQLiteDatabase.openDatabase(DBHelper.DB_PATH
            + DBHelper.DB_NAME, null, 0);
    Cursor localCursor = database
            .rawQuery(
                    "SELECT `rowid`,* FROM `TABLE1` WHERE 1=1 AND `FAV` LIKE '%TRUE%' ORDER BY `rowid` ASC LIMIT 0, 50000",
                    null);
    ArrayList<String> result = new ArrayList<String>();
    int TITLE = localCursor.getColumnIndex("TITLE");
  //↓ i want to access ID value onto OnClickUI method 
    ID = localCursor.getColumnIndex("ID");
    for (localCursor.moveToFirst(); !localCursor.isAfterLast(); localCursor
            .moveToNext()) {
        result.add(localCursor.getString(TITLE));
    }
    return result;
}

public void OnClickUI(View v) {
    switch (v.getId()) {
    case R.id.DelFromBookMark: {
       //Code to retrive ID of each Row
 .... .

 Toast.makeText(ActivityBookMark.this,v.getViewTreeObserver()+"",      Toast.LENGTH_SHORT).show();
        database.execSQL("UPDATE TABLE1 SET FAV='FALSE' WHERE ID=" + ID
                + "");
        List<String> newItems = geData();
        adapter.clear();
        adapter.addAll(newItems);
        adapter.notifyDataSetChanged();
        Toast.makeText(getApplicationContext(),
                "از لیست نشان شده ها حذف شد!", Toast.LENGTH_SHORT).show();

    }

        break;

    default:
        break;
    }
}

}

You should implement onItemclicklistner 您应该实现onItemclicklistner

   lv.setOnItemClickListener(new OnItemClickListener()
   {
      @Override
      public void onItemClick(AdapterView<?> adapter, View v, int position,
            long arg3) 
      {
            //in this method you get position and view of the item clicked the list


      }
   });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为Android中的ListView的每个项目设置背景图像? - How do I set a background image for each item of ListView in Android? 如何使用 firebase 获取 listView 中每个项目的数据? - How can I get the data for each item in the listView using firebase? Android ListView 如何获取每一行的值 - Android ListView how can I get the value of each individual row 如何使用 onclick 从 ListView 项目获取 URI? - How do get a URI from a ListView item with onclick? 如何在Android中从ListView正确删除项目? - How do I properly remove an item from ListView in Android? 当我从另一个活动获得结果时,ListView仅更新一个视图项,如何获得所有过去的结果? - ListView is updating only one view item when I get the results from another activity, how do I get all the past results? ListView中每行的颜色不同,我也想拖动该行,并且每行的背景颜色也要交换。 怎么做 - Different color for each row in a ListView, I want to drag the row and each row's background color exchange too. how to do it 如何根据每行的内容在ListView中更改行的颜色? - How do I change the color of a row in ListView depending on what is on each row? 我如何从ListView获取选定的项目? - How i can get selected item from ListView? 如何从下拉列表中添加项目,然后获取每个项目的文本 - How do i add items from a dropdown list then get the text of each item
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM