如何将onclick动态添加到arraylist
Posted
技术标签:
【中文标题】如何将onclick动态添加到arraylist【英文标题】:How to add onclick dynamically to arraylist 【发布时间】:2014-04-19 11:33:38 【问题描述】:我正在从 DB 获取标题列表,并使用 ArrayList
将这些标题添加到布局中。此布局是使用adapters
创建的。当我单击此布局中显示的标题时,我想打开另一个布局。我该如何解决这个问题?请有任何建议......
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.app.ListActivity;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class FullDetails extends ListActivity
ArrayList<String> listItems=new ArrayList<String>();
ArrayAdapter<String> adapter;
private ListView lv;
int clickCounter=0;
@Override
protected void onCreate(Bundle icicle)
super.onCreate(icicle);
setContentView(R.layout.activity_full_details);
adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listItems);
setListAdapter(adapter);
fullItems(null);
JSONArray all=new JSONArray();
String size;
public void fullItems(View v)
String result = "";
InputStream is=null;
try
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://MOTal.com/and/events.jsp");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
catch(Exception e)
Log.e("log_tag", "Error in http connection "+e.toString());
try
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
sb.append(line + "\n");
is.close();
result=sb.toString();
catch(Exception e)
Log.e("log_tag", "Error converting result "+e.toString());
try
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++)
JSONObject json_data = jArray.getJSONObject(i);
size=json_data.getString("size");
for(int j=0;j<Integer.parseInt(size);j++)
all=json_data.getJSONArray("res"+j);
listItems.add(all.get(0)+" \n venue:"+all.get(1)+" Price:"+all.get(3));
catch(JSONException e)
Log.e("log_tag", "Error parsing data "+e.toString());
adapter.notifyDataSetChanged();
activity_full_details.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_
android:layout_ >
<ListView
android:id="@android:id/list"
android:layout_
android:layout_
android:drawSelectorOnTop="false"
/>
</LinearLayout>
【问题讨论】:
添加 onClick 到 ArrayList ? 点击数组列表??我猜你想要一个 listview 行点击监听器。 @blackbelt,@raghunandan 感谢您的回复。如果可能的话onclick to arraylist
它解决了我的问题
我不知道。你有什么问题?
这里的jsonArray不打印任何数据ne.?
【参考方案1】:
您正在寻找的是列表项单击侦听器。你可以试试lv<your listview's obj>.setOnItemCLickListener
下面是给你的例子。
listView.setOnItemClickListener(new OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
Toast.makeText(getApplicationContext(),
"Click ListItem Number " + position, Toast.LENGTH_LONG)
.show();
);
关注这个基本example
没有像onClick
到ArrayList
这样的东西,点击是为查看而设置的。
【讨论】:
感谢您的回复。我按照我解决了这个问题的例子。【参考方案2】:设置好适配器后,还要设置“OnItemClickListener”:
getListView().setOnItemClickListener(new OnItemClickListener()
public void onItemClick(AdapterView<?> parent, View view, int position,
long id)
// Your logic when clicking. You can get the object for that row by
// calling parent.get(position) and casting to your data type in your adapter.
// which in this case seems to be string.
);
【讨论】:
【参考方案3】:在这个例子中以look为解决方案,认为是这样:
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener()
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
//Here put the code for each item. You now the position in the array because is the same as item.
【讨论】:
【参考方案4】:试试这个
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Toast.makeText(getApplicationContext(), "Test", Toast.LENGTH_LONG).show();
【讨论】:
以上是关于如何将onclick动态添加到arraylist的主要内容,如果未能解决你的问题,请参考以下文章