我需要帮助才能从listview获取当前项目并将数据传递给android studio中的其他活动 (我需要传递id,名称为register.java)进行注册巡视 谁能帮助我一步一步走出这个?
主要活动代码是:
public class MainActivity extends BaseActivity{
private static final String TAG = MainActivity.class.getSimpleName();
private ListView listView;
private FeedListAdapter listAdapter;
private List<FeedItem> feedItems;
private String URL_FEED = "http://host.com/android/index3.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list);
feedItems = new ArrayList<FeedItem>();
listAdapter = new FeedListAdapter(this, feedItems);
listView.setAdapter(listAdapter);
// We first check for cached request
Cache cache = AppController.getInstance().getRequestQueue().getCache();
Entry entry = cache.get(URL_FEED);
if (entry != null) {
// fetch the data from cache
try {
String data = new String(entry.data, "UTF-8");
try {
parseJsonFeed(new JSONObject(data));
} catch (JSONException e) {
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} else {
// making fresh volley request and getting json
JsonObjectRequest jsonReq = new JsonObjectRequest(Method.GET,
URL_FEED, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
VolleyLog.d(TAG, "Response: " + response.toString());
if (response != null) {
parseJsonFeed(response);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
}
});
// Adding request to volley request queue
AppController.getInstance().addToRequestQueue(jsonReq);
}
}
/**
* Parsing json reponse and passing the data to feed view list adapter
* */
private void parseJsonFeed(JSONObject response) {
try {
JSONArray feedArray = response.getJSONArray("feed");
for (int i = 0; i < feedArray.length(); i++) {
JSONObject feedObj = (JSONObject) feedArray.get(i);
FeedItem item = new FeedItem();
item.setId(feedObj.getInt("id"));
item.setName(feedObj.getString("name"));
// Image might be null sometimes
String image = feedObj.isNull("image") ? null : feedObj
.getString("image");
item.setImge(image);
item.setprice(feedObj.getString("price"));
item.setintro(feedObj.getString("intro"));
item.setdate1(feedObj.getString("date1"));
item.setdate2(feedObj.getString("date2"));
item.setvalance(feedObj.getString("valance"));
// url might be null sometimes
String feedUrl = feedObj.isNull("url") ? null : feedObj
.getString("url");
item.setUrl(feedUrl);
feedItems.add(item);
}
// notify data changes to list adapater
listAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, 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();
//noinspection SimplifiableIfStatement
switch (id)
{
case R.id.action_main: return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
}
和feeditem.java代码是:
public class FeedItem {
private int id;
private String name, price, image, intro, date1,date2,valance, url;
public FeedItem() {
}
public FeedItem(int id, String name, String image,String price, String intro,
String date1, String date2,String valance, String url) {
super();
this.id = id;
this.name = name;
this.image = image;
this.price = price;
this.intro = intro;
this.date1 = date1;
this.date2 = date2;
this.valance = valance;
this.url = url;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImge() {
return image;
}
public void setImge(String image) {
this.image = image;
}
public String getprice() {
return price;
}
public void setprice(String price) {
this.price = price;
}
public String getintro() {return intro;}
public void setintro(String intro) {
this.intro = intro;
}
public String getdate1() {
return date1;
}
public void setdate1(String date1) {
this.date1 = date1;
}
public String getdate2() {
return date2;
}
public void setdate2(String date2) {
this.date2 = date2;
}
public String getvalance() {
return valance;
}
public void setvalance(String valance) {
this.valance = valance;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
答案 0 :(得分:0)
在onCreate()
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list);
...
...
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, YOUR_ACTIVITY.class); //YOUR_ACTIVITY is where you want to pass data
FeedItem feedItem = (FeedItem) listView.getItemAtPosition(position);
intent.putExtra("feed_item", feedItem);
startActivity(intent);
}
});
...
...
}
在YOUR_ACTIVITY Activity
中,从下面的
public class YOUR_ACTIVITY extends AppCompatActivity{
...
...
@Override
protected void onCreate(Bundle savedInstanceState) {
FeedItem feedItem = (FeedItem) getIntent().getSerializableExtra("feed_item");
...
...
}
}
不要忘记在Serializable
课程中实施FeedItem
。
public class FeedItem implements Serializable {
...
...
}
答案 1 :(得分:0)
我跟踪这段代码,但是当我点击Item list Nothing Happen时。 首先我的问题是我不知道使用setOnItemClickListener方法。 我没有错误,没有警告,但setOnItemClickListener不工作。 什么问题?