我的ListView正在打开,一切正常。我不知道如何从onPostExecute()传递params到onItemClick()以通过id打开一个新的活动(SingleItem.java)。 我所尝试的一切都没有奏效。
ListItems.java
public class ListItems extends Activity {
private ListView listV;
TextView estado, cidade, noItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_items);
listV = (ListView) findViewById(R.id.listV);
estado = (TextView) findViewById(R.id.Estado);
cidade = (TextView) findViewById(R.id.Cidade);
noItem = (TextView) findViewById(R.id.noItem);
estado.setText(getIntent().getExtras().getString("state"));
cidade.setText(getIntent().getExtras().getString("city"));
Task task = new Task();
task.execute();
listV.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(getApplicationContext(), SingleItem.class);
startActivity(intent);
}
});
}
public class Task extends AsyncTask<String, String, Void>{
private ProgressDialog progressDialog = new ProgressDialog(ListItems.this);
InputStream is = null;
String result = "";
protected void onPreExecute() {
progressDialog.setMessage("Listing Items...");
progressDialog.show();
progressDialog.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
Task.this.cancel(true);
}
});
};
@Override
protected Void doInBackground(String... params) {
String url = "http://myip/webviews/jsonlistItems.php";
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();
try {
httpPost.setEntity(new UrlEncodedFormEntity(param));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error connecting to database " + e.toString());
Toast.makeText(ListItems.this, "Try again.", Toast.LENGTH_LONG).show();
}
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = "";
while((line = br.readLine()) != null){
sb.append(line+"\n");
}
is.close();
result = sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result " + e.toString());
}
return null;
}
protected void onPostExecute(Void v){
try {
JSONArray Jarray = new JSONArray(result);
for (int i = 0; i < Jarray.length(); i++) {
JSONObject jsonObject = null;
jsonObject = Jarray.getJSONObject(i);
// output
String item_id = jsonObject.getString("item_id");
String item_name = jsonObject.getString("item_name");
String item_color = jsonObject.getString("item_color");
String city = jsonObject.getString("city");
String statee = jsonObject.getString("state");
if(estado.getText().toString().equalsIgnoreCase(statee) &&
cidade.getText().toString().equalsIgnoreCase(city)){
String[] values = new String[] {item_name, item_color};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(ListItems.this, android.R.layout.simple_list_item_1, values);
listV.setAdapter(adapter);
break;
}
else{
noItem.setText("No Item to show");
}
}
this.progressDialog.dismiss();
} catch (Exception e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
}
}
public class ItemById{
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.list_events, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
SingleItem.java
public class SingleItem extends Activity {
TextView item_name, item_color;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_singleitem);
item_name = (TextView) findViewById(R.id.item_name);
item_color = (TextView) findViewById(R.id.item_color);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.event, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
如何从onPostExecute()传递params到onItemClick()以通过id打开一个新活动(SingleItem.java)?
答案 0 :(得分:0)
适配器看起来只是由JSON对象的每个属性填充(item_name为一行,item_color为一行)。如果这是你想要的,你自然不会得到item_id,因为它不存在。
如果希望每行对应于每个JSON对象,则应修改适配器。
首先制作自己的类
class Wrap{
String itemId, itemName, city, statee;
}
然后创建自己的适配器类,扩展ArrayAdapter<Wrap>
。
这种方式每次单击一行时,都可以获得包含所有内容的Wrap对象,包括id。然后,您可以将这些值传递给下一个活动。
答案 1 :(得分:0)
这完全取决于您的SingleItem Activity在打开时应显示的内容。 如果您只需要所选项目的名称,则只需在onItemClick方法中检索项目名称并将其作为参数传递给SingleItem:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getApplicationContext(), SingleItem.class);
String name = adapter.getItem(position);
intent.putExtra("yourItem", name);
startActivity(intent);
}
为了使其工作,适配器需要是Activity中的变量:
public class ListItems extends Activity {
private ArrayAdapter<String> adapter;
和此:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(ListItems.this, android.R.layout.simple_list_item_1, values);
成为这个:
adapter = new ArrayAdapter<String>(ListItems.this, android.R.layout.simple_list_item_1, values);
如果您需要SingleItem Activity中的更多信息,而不仅仅是项目名称,您必须创建一个Item类来保存该信息:
public static class Item implements Serializable {
String mName;
String mColor;
// more data
@Override
public String toString() {
return mName;
}
}
你的onItemClick变为:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getApplicationContext(), SingleItem.class);
Item item = adapter.getItem(position);
intent.putExtra("yourItem", name);
startActivity(intent);
}
您的适配器将是:
ArrayAdapter<Item> adapter;
adapter = new ArrayAdapter<Item>(ListItems.this, android.R.layout.simple_list_item_1, itemArray);
当然,在解析json流时你需要创建itemArray。
活动可以轻松地从意图中读取所选项目,如下所示:
getIntent().getSerializableExtra("yourItem");