Android 筛选的ListView onItemClick返回在线活动原始位置的项目
您好,我正在尝试从Android 筛选的ListView onItemClick返回在线活动原始位置的项目,android,listview,listview-filter,Android,Listview,Listview Filter,您好,我正在尝试从网站制作一个包含一些带有自动建议的列表项的应用程序。但是,当我单击其中一个项目时,它只返回与第一个项目相关的值。但我只想从他们那里得到相关值 这是我的主要活动: public class MainActivity extends AppCompatActivity { ListView vl; Button b; // List view private ListView lv; // Listview Adapter Ar
网站
制作一个包含一些带有自动建议的列表项
的应用程序。但是,当我单击其中一个项目时,它只返回与第一个项目相关的值。但我只想从他们那里得到相关值
这是我的主要活动
:
public class MainActivity extends AppCompatActivity {
ListView vl;
Button b;
// List view
private ListView lv;
// Listview Adapter
ArrayAdapter<String> adapter;
// Search EditText
EditText inputSearch;
// ArrayList for Listview
ArrayList<HashMap<String, String>> news_title;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vl = (ListView) findViewById(R.id.myList);
b= (Button) findViewById(R.id.addNewbutton);
inputSearch = (EditText) findViewById(R.id.inputSerach);
fetchingData();
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(), AddingNews.class));
}
});
}
void fetchingData(){
String myURL = "http://192.168.56.1/android/api/gettingnews.php";
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(myURL, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
final String[] news_title = new String[response.length()];
final String[] news_detail = new String[response.length()];
final String[] news_time = new String[response.length()];
for (int i =0; i < response.length(); i++){
try {
JSONObject jsonObject = (JSONObject) response.get(i);
news_title[i] = jsonObject.getString("title");
news_detail[i] = jsonObject.getString("news");
news_time[i] = jsonObject.getString("time");
} catch (JSONException e) {
e.printStackTrace();
}
}
// vl.setAdapter(new ArrayAdapter(getApplicationContext(), R.layout.mylistview, R.id.textViewforlist, news_title));
// Adding items to listview
adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.mylistview, R.id.textViewforlist, news_title);
vl.setAdapter(adapter);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
MainActivity.this.adapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
vl.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, Details.class);
intent.putExtra("MyTitle", news_title[position]);
intent.putExtra("MyNews", news_detail[position]);
intent.putExtra("MyTime", news_time[position]);
startActivity(intent);
}
});
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("Volley Log", error);
}
});
com.example.forkan.tuntuninews.AppController.getInstance().addToRequestQueue(jsonArrayRequest);
Toast.makeText(getApplicationContext(),"Data Loaded Successfully", Toast.LENGTH_LONG).show();
}
}
如何修复它请帮助我..可能的副本,但我可以在哪里使用它adapter@ForkanAkon你需要声明你自己的适配器(它应该扩展BaseAdapter
)。如果你给出一个链接或示例,对我会非常有帮助
public class Details extends AppCompatActivity {
TextView title, time, details;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
title = (TextView) findViewById(R.id.mytitle);
time = (TextView) findViewById(R.id.mytime);
details = (TextView) findViewById(R.id.mydetail);
String _title = getIntent().getStringExtra("MyTitle");
String _news = getIntent().getStringExtra("MyNews");
String _time = getIntent().getStringExtra("MyTime");
title.setText(_title);
time.setText(_time+"\n\n");
details.setText(_news);
}
}