![](/img/trans.png)
[英]how to enable check box single checked when i click on item of list view item
[英]Single Item Click in a list
在支持下,我創建了一個通過JSON數據填充的數組的列表,並且當用戶單擊某個項目時,該列表將它們帶到一個活動頁面,該頁面為他們提供了有關它的更多信息。 我已嘗試完成此操作,但到目前為止效果並不理想。 從某種意義上說,數組列表確實可以成功顯示,但是當單擊某個項目時,會意外顯示空白活動頁面。
以下是數組活動代碼列表:
public class EventsActivity extends Activity{
private static final String URL_WEB_SERVICE = "http://dooba.ca/analytics/ed.php";
private GridView gv;
private ArrayList<Events_List> container;
private ArrayList<Events_List> items;
public Uri list_item_bac;
public String list_item_name;
public String list_item_description;
public String list_item_price;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.events_list_layout);
gv = (GridView) findViewById(R.id.gridview);
container = new ArrayList<Events_List>();
//download JSON
listDownload();
GridView s = (GridView) findViewById(R.id.gridview);
s.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(EventsActivity.this,EventSingleItemActivity.class);
intent.putExtra("list_item_name", list_item_name);
intent.putExtra("list_item_description", list_item_description);
intent.putExtra("list_item_price",list_item_price);
startActivity(intent); //start Activity
}
});
}
public void listDownload(){
RequestQueue volley = Volley.newRequestQueue(this);
JsonObjectRequest json = new JsonObjectRequest(Method.GET, URL_WEB_SERVICE, null, ResponseListener(), ErrorListener());
volley.add(json);
}
private Response.Listener<JSONObject> ResponseListener() {
return new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
//your JSON Array
JSONArray array = response.getJSONArray("list_item");
for(int i = 0; i < array.length(); i++){
container.add(convertirAnuncio(array.getJSONObject(i)));
}
} catch (JSONException e) {
e.printStackTrace();
}
gv.setAdapter(new AdapterEvents(getApplicationContext(),container));
}
};
};
private Response.ErrorListener ErrorListener() {
return new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) { }
};
}
//object JSON
private final Events_List convertirAnuncio(JSONObject obj) throws JSONException {
long id = obj.getLong("id"); //id
String list_item_name = obj.getString("list_item_name");
String list_item_description = obj.getString("list_item_description");
String list_item_price = obj.getString("list_item_price");
Uri uri = Uri.parse(obj.getString("list_item_bac"));
return new Events_List(id,list_item_name,list_item_description,list_item_price, uri);
}
}
以下是單項點擊
public class EventSingleItemActivity extends Activity {
// Declare Variables
String list_item_name;
String list_item_description;
String list_item_price;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_events_single_item);
Intent i = getIntent();
list_item_name = i.getStringExtra("list_item_name");
list_item_description = i.getStringExtra("list_item_description");
list_item_price = i.getStringExtra("list_item_price");
TextView txtname = (TextView) findViewById(R.id.name);
TextView txtdescription = (TextView) findViewById(R.id.description);
TextView txtprice = (TextView) findViewById(R.id.price);
// Set results to the TextViews
txtname.setText(list_item_name);
txtdescription.setText(list_item_description);
txtprice.setText(list_item_price);
}
以下是單項XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/name" />
<TextView
android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/price" />
<ImageView
android:id="@+id/image_head"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#000000"
android:padding="1dp" />
</RelativeLayout>
}
更新
public class EventsActivity extends Activity{
private static final String URL_WEB_SERVICE = "http://dooba.ca/analytics/ed.php";
private GridView gv;
private ArrayList<Events_List> container;
private ArrayList<Events_List> items;
public Uri list_item_bac;
public String list_item_name;
public String list_item_description;
public String list_item_price;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.events_list_layout);
gv = (GridView) findViewById(R.id.gridview);
container = new ArrayList<Events_List>();
//download JSON
listDownload();
GridView s = (GridView) findViewById(R.id.gridview);
s.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(EventsActivity.this,EventSingleItemActivity.class);
intent.putExtra("list_item_name", container.get(position).getList_item_name);
intent.putExtra("list_item_description", list_item_description);
intent.putExtra("list_item_price",list_item_price);
startActivity(intent); //start Activity
}
});
}
public void listDownload(){
RequestQueue volley = Volley.newRequestQueue(this);
JsonObjectRequest json = new JsonObjectRequest(Method.GET, URL_WEB_SERVICE, null, ResponseListener(), ErrorListener());
volley.add(json);
}
private Response.Listener<JSONObject> ResponseListener() {
return new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
//your JSON Array
JSONArray array = response.getJSONArray("list_item");
for(int i = 0; i < array.length(); i++){
container.add(convertirAnuncio(array.getJSONObject(i)));
}
} catch (JSONException e) {
e.printStackTrace();
}
gv.setAdapter(new AdapterEvents(getApplicationContext(),container));
}
};
};
private Response.ErrorListener ErrorListener() {
return new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) { }
};
}
//object JSON
private final Events_List convertirAnuncio(JSONObject obj) throws JSONException {
long id = obj.getLong("id"); //id
String list_item_name = obj.getString("list_item_name");
String list_item_description = obj.getString("list_item_description");
String list_item_price = obj.getString("list_item_price");
Uri uri = Uri.parse(obj.getString("list_item_bac"));
return new Events_List(id,list_item_name,list_item_description,list_item_price, uri);
}
}
Any help would be greatly appreciated.
更新2事件列表活動
public class Events_List {
public long id;
public String list_item_title;
public String list_item_price;
public String list_item_description;
public Uri url;
public Events_List(long id, String list_item_title, String list_item_description, String list_item_price, Uri url){
this.id = id;
this.list_item_title = list_item_title;
this.list_item_description = list_item_description;
this.list_item_price = list_item_price;
this.url = url;
}
}
問題:
intent.putExtra("list_item_name", list_item_name);
intent.putExtra("list_item_description", list_item_description);
intent.putExtra("list_item_price",list_item_price);
這些值是空的,因為它們從未使用過或未初始化,因此,當您切換活動並從意圖中獲取值時,它們將返回空字符串,因此沒有結果
解:
使用您的arraylist
對象container
並使用onItemClick
的位置,並使用arraylist
的get方法獲取值
樣品:
intent.putExtra("list_item_name", container.get(position).getList_item_name);
記住,你的類Events_List
必須有一個getter方法List_item_name
更新:
public class Events_List {
public long id;
public String list_item_title;
public String list_item_price;
public String list_item_description;
public Uri url;
public Events_List(long id, String list_item_title, String list_item_description, String list_item_price, Uri url){
this.id = id;
this.list_item_title = list_item_title;
this.list_item_description = list_item_description;
this.list_item_price = list_item_price;
this.url = url;
}
public String getList_item_title()
{
return this.list_item_title;
}
public String getList_item_price()
{
return this.list_item_price;
}
public String getList_item_description()
{
return this.list_item_description;
}
}
如何使用它:
intent.putExtra("list_item_name", container.get(position).getList_item_title());
intent.putExtra("list_item_description", container.get(position).getList_item_description());
intent.putExtra("list_item_price",container.get(position).getList_item_description());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.