如何从列表视图android的价值? [英] How to get values from listview android?
本文介绍了如何从列表视图android的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在android.I新我从MySQL中获取数据并显示在listview.things它工作正常,但现在我想的值传递给意向时列表视图(行)的用户点击。我已经实现setOnItemClickListener.but列表视图是动态的,所以我没有得到如何得到的值,并将它传递给意向。
在此先感谢
公共类AdminNotice延伸活动{ 私有String [] navMenuTitles;
私人TypedArray navMenuIcons;
私人的EditText editTextName;
共享preferences SP;
私人字符串jsonResult;
私人的ListView ListView的;
私人键b;
的EditText etname等;
TextView的电视;
串myJSON;
私有静态最后弦乐TAG =MainActivity.java;
私有静态最后弦乐TAG_NAME =通知;
私有静态最后弦乐TAG_DATE =ndate;
进度进度;
日期日期;
JSONArray人民= NULL; ArrayList的<&HashMap的LT;字符串,字符串>> personList; ListView控件列表; 公共静态最后弦乐USER_NAME =USERNAME; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.noticelist);
进度=(进度)findViewById(R.id.progressbar);
//共享preferences我的preFS = getShared preferences(用户,MODE_WORLD_READABLE);
//字符串SESSION_ID =我的prefs.getString(SESSION_ID,NULL);
// TextView中的TextView =(的TextView)findViewById(R.id.fname); //textView.setText(\"Welcome+ SESSION_ID); navMenuTitles = getResources()getStringArray(R.array.nav_drawer_items)。 navMenuIcons = getResources()obtainTypedArray(R.array.nav_drawer_icons)。
从//加载图标
//的strings.xml
名单=(ListView控件)findViewById(R.id.listView);
personList =新的ArrayList<&HashMap的LT;字符串,字符串>>(); 的getData();
} //发送信息停止 //获取JSON数据开始
保护无效showList(){
尝试{
JSONArray人民=新JSONArray(myJSON);
的for(int i = 0; I< peoples.length();我++){
JSONObject的C = peoples.getJSONObject(I)
字符串名称= NULL,日期= NULL; / *如果(C == NULL){
ProgressDialog进度=新ProgressDialog(本);
progress.setTitle(加载);
progress.setMessage(稍等...);
progress.show();
} * / 如果(c.has(通知))
如果(c.has(ndate))
progressBar.setVisibility(View.GONE);
名称= c.getString(通知);
日期= c.getString(ndate);
HashMap的<字符串,字符串>人=新的HashMap<字符串,字符串>();
persons.put(TAG_NAME,名);
persons.put(TAG_DATE,日期);
personList.add(人);
} ListAdapter适配器=新SimpleAdapter(
AdminNotice.this,personList,R.layout.list_item1,
新的String [] {TAG_NAME,TAG_DATE},
新的INT [] {R.id.name,R.id.date}
); list.setAdapter(适配器); list.setOnItemClickListener(新AdapterView.OnItemClickListener(){ @覆盖
公共无效onItemClick(适配器视图<>适配器视图,视图观点,INT位置,
长ID){ / * ModelClass OBJ =的getItem(位置);
字符串名称= obj.getName(); * /
//简单敬酒,显示位置选择
Log.d(SELECT_POSITION,位置这个列表项目=+位置);
} }); / * list.setOnItemClickListener(新AdapterView.OnItemClickListener(){ 公共无效onItemClick(适配器视图父母,观景,INT位置,长的id){
Toast.makeText(getApplicationContext(),((TextView的)视图).getText(),Toast.LENGTH_SHORT).show(); } }); * / }赶上(JSONException E){
Log.i(tagconvertstr,[+ myJSON +]);
}
} 公共无效的getData(){
类GetDataJSON扩展的AsyncTask<弦乐,太虚,字符串> {
@覆盖
保护字符串doInBackground(字符串... PARAMS){ 共享preferences我的preFS = getShared preferences(用户,MODE_WORLD_READABLE);
字符串的session_id =我的prefs.getString(SESSION_ID,NULL); 为InputStream的InputStream = NULL;
字符串结果= NULL;
尝试{ 字符串postReceiverUrl =HTTP://notice.php // HttpClient的
HttpClient的HttpClient的=新DefaultHttpClient(); //头后
HttpPost httpPost =新HttpPost(postReceiverUrl); //添加数据
清单<&的NameValuePair GT; namevaluepairs中=新的ArrayList<&的NameValuePair GT;(2);
nameValuePairs.add(新BasicNameValuePair(用户名,SESSION_ID)); httpPost.setEntity(新UrlEn codedFormEntity(namevaluepairs中));
HTT presponse响应= httpClient.execute(httpPost);
HttpEntity resEntity = response.getEntity(); 的InputStream = resEntity.getContent();
// JSON默认为UTF-8
读者的BufferedReader =新的BufferedReader(新的InputStreamReader(InputStream中,UTF-8),8);
StringBuilder的SB =新的StringBuilder(); 串线= NULL;
而((行= reader.readLine())!= NULL)
{
sb.append(行+\\ n);
}
结果= sb.toString();
}赶上(例外五){
Log.i(tagconvertstr,[+导致+]);
的System.out.println(E);
}
最后{
尝试{如果(InputStream的!= NULL)inputStream.close();}赶上(例外挤){}
}
返回结果;
} @覆盖
保护无效onPostExecute(字符串结果){
myJSON =结果;
showList();
}
}
GetDataJSON G =新GetDataJSON();
g.execute();
}
//获取JSON数据停止}
解决方案
在您的ListView onItemClickListener
意向意图=新意图(currentyactivty.this,secondactiviy.class);
intent.putExtra(TAG_NAME,personList.get(位置)获得(TAG_NAME));
startActivity(意向);
要在第二个活动在onCreate方法GETDATA
字符串数据;
在意向= getIntent();如果(在= NULL和放大器;!&安培; in.hasExtra(TAG_NAME)){
数据= in.getStringArrayExtra(TAG_NAME);
}
I am new in android.I am fetching data from mysql and showing it in listview.things are working fine but now i want to pass the value to intent when user click on listview(Row).I have implemented setOnItemClickListener.but the list view is dynamic so i am not getting how to get values and pass it to the intent.
Thanks in advance
public class AdminNotice extends Activity {
private String[] navMenuTitles;
private TypedArray navMenuIcons;
private EditText editTextName;
SharedPreferences sp;
private String jsonResult;
private ListView listView;
private Button b;
EditText etname, et;
TextView tv;
String myJSON;
private static final String TAG = "MainActivity.java";
private static final String TAG_NAME = "notice";
private static final String TAG_DATE = "ndate";
ProgressBar progressBar;
Date date;
JSONArray peoples = null;
ArrayList<HashMap<String, String>> personList;
ListView list;
public static final String USER_NAME = "USERNAME";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.noticelist);
progressBar = (ProgressBar) findViewById(R.id.progressbar);
//SharedPreferences myprefs= getSharedPreferences("user", MODE_WORLD_READABLE);
// String session_id= myprefs.getString("session_id", null);
//TextView textView = (TextView) findViewById(R.id.fname);
//textView.setText("Welcome "+session_id);
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);
// load icons from
// strings.xml
list = (ListView) findViewById(R.id.listView);
personList = new ArrayList<HashMap<String,String>>();
getData();
}
//send messages stop
//get json data start
protected void showList(){
try {
JSONArray peoples = new JSONArray(myJSON);
for(int i=0;i<peoples.length();i++){
JSONObject c = peoples.getJSONObject(i);
String name=null, date=null;
/*if(c==null){
ProgressDialog progress = new ProgressDialog(this);
progress.setTitle("Loading");
progress.setMessage("Wait while loading...");
progress.show();
}*/
if(c.has("notice"))
if(c.has("ndate"))
progressBar.setVisibility(View.GONE);
name = c.getString("notice");
date = c.getString("ndate");
HashMap<String,String> persons = new HashMap<String,String>();
persons.put(TAG_NAME,name);
persons.put(TAG_DATE,date);
personList.add(persons);
}
ListAdapter adapter = new SimpleAdapter(
AdminNotice.this, personList, R.layout.list_item1,
new String[]{TAG_NAME,TAG_DATE},
new int[]{R.id.name, R.id.date}
);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position,
long id) {
/*ModelClass obj = getItem(position);
String name = obj.getName();*/
// Simple Toast to show the position Selected
Log.d("SELECT_POSITION", "Position For this List Item = " + position);
}
});
/* list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});*/
} catch (JSONException e) {
Log.i("tagconvertstr", "["+myJSON+"]");
}
}
public void getData(){
class GetDataJSON extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
SharedPreferences myprefs= getSharedPreferences("user", MODE_WORLD_READABLE);
String session_id= myprefs.getString("session_id", null);
InputStream inputStream = null;
String result = null;
try {
String postReceiverUrl = "http://notice.php";
// HttpClient
HttpClient httpClient = new DefaultHttpClient();
// post header
HttpPost httpPost = new HttpPost(postReceiverUrl);
// add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", session_id));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity resEntity = response.getEntity();
inputStream = resEntity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
Log.i("tagconvertstr", "["+result+"]");
System.out.println(e);
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
return result;
}
@Override
protected void onPostExecute(String result){
myJSON = result;
showList();
}
}
GetDataJSON g = new GetDataJSON();
g.execute();
}
//get json data stop
}
解决方案
Inside your listview onItemClickListener
Intent intent=new Intent(currentyactivty.this,secondactiviy.class);
intent.putExtra("TAG_NAME", personList.get(position).get(TAG_NAME));
startActivity(intent);
To getdata in second activity at onCreate method
String data;
Intent in=getIntent();
if(in!=null && in.hasExtra("TAG_NAME")){
data=in.getStringArrayExtra("TAG_NAME");
}
这篇关于如何从列表视图android的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文