我试图从点击的ListView项中获取一些值,这样我就可以使用ListView项目所拥有的相同标题和描述打开我的新意图。
我这样做有点麻烦,因为直到最近我还没有使用过ListViews。请问有人帮我解决这个问题吗?
SocietySearch.java:
特定代码位于OnItemClick
public class SocietySearch extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) { //TODO remember notifyDataSetChanged
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_society_search);
List<Society> societies = new ArrayList<>();
ServerRequests serverRequest1 = new ServerRequests(SocietySearch.this);
serverRequest1.GetSocietyDataAsyncTask(societies, new GetSocietyCallback() {
@Override
public void done(final List<Society> societies) {
final ListView lv = (ListView) findViewById(R.id.ListView);
List<ListViewItem> items = new ArrayList<>();
for (final Society s : societies) {
items.add(new ListViewItem() {{
ThumbnailResource = R.drawable.test;
Title = s.socName;
Description = s.socDes;
}});
}
final CustomListViewAdapter adapter = new CustomListViewAdapter(SocietySearch.this, items);
lv.setAdapter(adapter);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) findViewById(R.id.search);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setQueryHint("Search..");
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String text) {
adapter.getFilter().filter(text);
return false;
}
});
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
Toast.makeText(getApplicationContext(), "position: " + position, Toast.LENGTH_LONG).show();
Intent societyPage = new Intent(SocietySearch.this, SocietyPage.class);
startActivity(societyPage);
List<ListViewItem> obj = items.getItem(position);
//Here I want to get all values from clicked item position and assign them to variables
String value = obj.toString();
Log.d("MyLog", "Value is: " + value);
}
});
}
});
}
public class ListViewItem {
public int ThumbnailResource;
public String Title;
public String Description;
@Override
public String toString() {
return Title;
}
}
答案 0 :(得分:1)
首先,我相信以下不会返回列表。它将返回一个ListViewItem对象。
List<ListViewItem> obj = items.getItem(position);
如果我理解你的问题。您将需要获取对象并将这些值传递给intent。以下示例。
ListViewItem item = items.getItem(position);
String title = item.Title;
String desc = item. Description;
int thumbnail = item. ThumbnailResource;
Intent societyPage = new Intent(SocietySearch.this, SocietyPage.class);
i.putExtra("title", title);
i.putExtra("desc", desc);
i.putExtra("thumbnail", thumbnail);
startActivity(societyPage);