【问题标题】:getting String from ArrayList从 ArrayList 获取字符串
【发布时间】:2013-07-12 09:17:03
【问题描述】:

我正在从数据库中获取数据并将其放入一个数组中。数组包含代码(int)和说明(字符串)。代码在 gridview 中,我希望如果有人点击代码,那么真实的描述应该显示在一个新的活动。我使用了 OnItemclicklistener 但不知道如何从数组中仅获取字符串以在新活动中显示它。 “我希望 arraylist 中的代码应该打印在第一个屏幕上,当点击描述时,将在新活动中作为 textview 打印”

 private void show_data() {
    final GridView gv = (GridView) findViewById(R.id.gridView);
  //  final ListView listview = (ListView) findViewById(R.id.listView1);
    final Database_handler db = new Database_handler(this);
    Cursor cur = db.fetchAllData();
    final ArrayList<String> array_list = new ArrayList<String>();
    if (cur.moveToFirst()) {
        do {
            array_list.add(cur.getString(0));
            array_list.add(cur.getString(1));
        } while (cur.moveToNext());
    }
    final StableArrayAdapter adapter = new StableArrayAdapter(this,
            android.R.layout.simple_list_item_1, array_list);
   // listview.setAdapter(adapter);
    gv.setAdapter(adapter);

    gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                                int position, long id) {
                    /*SOME CODE*/ }

这是 Stablearrayadapter 类

   public class StableArrayAdapter extends ArrayAdapter<String> {
 HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

public StableArrayAdapter(Context context, int textViewResourceId, List<String> objects) {
    super(context, textViewResourceId, objects);
    for (int i = 0; i < objects.size(); ++i) {
        mIdMap.put(objects.get(i), i);
    }
}

XML 类

  <?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="match_parent">

<TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView_descrip"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="70dp"
        android:layout_alignParentTop="true"
        android:layout_marginTop="50dp"/>
            </RelativeLayout>

描述活动

 public class desc_view extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);    //To change body of overridden methods use File | Settings | File Templates.
    setContentView(R.layout.desc_view);
   /* try {
        Intent recv = getIntent();
        ArrayList<String> array_list_recvd = recv.getStringArrayListExtra("array_list2");
        int intValue = recv.getIntExtra("intVariableName", 0);
        TextView tv = (TextView) findViewById(R.id.textView_descrip);
        tv.setText(array_list_recvd.get(intValue));
        setContentView(tv);
    } catch (Exception e) {
        Log.d("Error Second :", e.toString());
    }*/
}

}

【问题讨论】:

    标签: android arraylist android-arrayadapter


    【解决方案1】:

    你应该使用Map&lt;String,String&gt;的arraylist

     ArrayList<Map<String,String>> array_list = new ArrayList<Map<String,String>>();
    
    if (cur.moveToFirst()) {
        do {
            Map<String,String> map=new HashMap<String,String>();
            map.put("code",cur.getString(0));
            map.put("desc",cur.getString(1));
    
            array_list.add(map);
    
        } while (cur.moveToNext());
    }
    

    在 itemClick 上

    String desc=list.get(position).get("desc");
    

    【讨论】:

    • 尝试在游标中打印数据...while循环可能为空并检查您是否使用了正确的arraylist
    猜你喜欢
    • 2017-11-29
    • 2012-04-04
    • 1970-01-01
    • 2013-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多