[英]I want to display the contents of that specific text view when i click on the text view on a different activity. I
Here is my main activity Collegelist with the contents already loaded from the database but i have only displayed the name.When i click on the name i want to show the name,address and contact on different activity. 这是我的主要活动Collegelist,其内容已经从数据库加载,但是我只显示了名称。当我单击名称时,我想显示名称,地址和其他活动的联系方式。
public class Collegelist extends ActionBarActivity {
HTTPConnection http;
List<Colleges> college = new ArrayList<Colleges>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.college_list);
http = new HTTPConnection(getApplicationContext());
if (http.isNetworkConnection()) {
//String data = http.HTTPGetData("http://localhost/minorproject/show.php");
//Toast.makeText(getApplicationContext(),data ,Toast.LENGTH_LONG).show();
task.execute();
}
else {
Toast.makeText(getApplicationContext(), "check your connection",
Toast.LENGTH_LONG).show();
}
}
AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
String data = http.HTTPGetData("http://localhost/college/show.php");
return data;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
populateList(result);
displayList();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
};
protected void populateList(String result) {
try {
//Toast.makeText(getApplicationContext(),result,Toast.LENGTH_LONG).show();
JSONObject jobj = new JSONObject(result);
String res = jobj.getString("success");
if (!res.equals("true")) {
Toast.makeText(getApplicationContext(), "JSON Error",
Toast.LENGTH_LONG).show();
return;
}
else
{
JSONArray data = jobj.getJSONArray("msg");
// Toast.makeText(getApplicationContext(),"successss",Toast.LENGTH_SHORT).show();
for (int i = 0; i < data.length(); i++) {
JSONObject col = data.getJSONObject(i);
Colleges cg = new Colleges(col.getString("cname"), col.getString("caddress"), col.getString("ccontact_a"));
college.add(cg);
}
}
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
}
protected void displayList() {
ArrayAdapter<Colleges> adapter = new ArrayAdapter<Colleges>(this, R.layout.list_item,college){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = getLayoutInflater().inflate(R.layout.list_item,null);
//set values
Colleges c = college.get(position);
((TextView)view.findViewById(R.id.name)).setText(c.getName());
/* ((TextView)view.findViewById(R.id.address)).setText(c.getAddress());
((TextView)view.findViewById(R.id.contact)).setText(c.getContact());
*/
return view;
}
};
final ListView collegelistnew = (ListView) findViewById(R.id.listView);
collegelistnew.setAdapter(adapter);
collegelistnew.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
/*Toast.makeText(
getApplicationContext(),
"You clicked position" + position + "with item name"
+ college.get(position).getName(),
Toast.LENGTH_LONG).show();*/
Intent newIntent =new Intent(getApplicationContext(),CollegeDetails.class);
newIntent.putExtra("college", (Serializable) college.get(position));
startActivity(newIntent);
}
});
}}
Uncomment these two lines : 取消注释这两行:
/* ((TextView)view.findViewById(R.id.address)).setText(c.getAddress());
((TextView)view.findViewById(R.id.contact)).setText(c.getContact());
*/
Then set their visibility to GONE using: 然后使用以下命令将其可见性设置为GONE:
((TextView)view.findViewById(R.id.address)).setVisibility(View.GONE);
((TextView)view.findViewById(R.id.contact)).setVisibility(View.GONE);
or using xml. 或使用xml。
And in this way you can show only one name in your list view but you get the address and contact also to carry on to another activty. 这样,您可以在列表视图中仅显示一个名称,但是您可以获取地址并联系以继续进行另一项活动。
That class seems to be OK. 那堂课似乎还可以。 However you should make your class
Colleges
to implement Parcelable
interface. 但是,您应该使您的班级
Colleges
实现Parcelable
接口。
The error must be in CollegeDetails
class 该错误必须在
CollegeDetails
类中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.