0
我從數據庫中獲取數據並將其放入數組中。數組包含代碼(int)和描述(字符串)。代碼位於gridview中,我希望如果有人單擊代碼,則實際描述應該顯示在一個新的activity.i已經使用OnItemclicklistener,但不知道如何從數組中只獲取字符串顯示一個新的活動。 「我想,從ArrayList中的代碼應該第一個屏幕上進行印刷,並點擊時的說明會在新的活性的TextView打印」從ArrayList獲取字符串
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>
activty爲了描述
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());
}*/
}
}
它不會顯示任何東西在GridView的@Samir甚至代碼。 –
嘗試在遊標中打印數據do ... while循環可能爲空並檢查您是否使用右列陣列表 –