为listview设置标题

时间:2016-04-19 08:48:29

标签: android listview

我有一个Android应用程序应该解析来自php网页的数据并将它们显示为两个listview,每个listview显示一个String的值

我的问题是如何为这些列表视图设置标题?

-lpcre16

activity_main.xml中

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState)
 {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 new GetData().execute();
 }

 private class GetData extends AsyncTask<Void, Void, Void> 
 {
  ProgressDialog progressDialog;
  String data;
  List<String> r = new ArrayList<String>();
  List<String> r2 = new ArrayList<String>();
  ArrayAdapter<String>adapter=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,r);    
  ArrayAdapter<String>adapter2=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,r2);
  ListView list =  (ListView)findViewById(R.id.right);
  ListView list2= (ListView)findViewById(R.id.left); 

 public   GetData()
 {  }

 @Override
 protected void onPreExecute()
 {
 super.onPreExecute();
 list.setBackgroundColor(color.background_dark);
 list2.setBackgroundColor(color.background_dark);
 progressDialog = new ProgressDialog(MainActivity.this);
 progressDialog.setCancelable(false);
 if (!progressDialog.isShowing()) 
 {
 progressDialog.show();
  } 
        }

 @Override
 protected Void doInBackground(Void... params) 
 {
 try 
 {
 DefaultHttpClient client = new DefaultHttpClient();
 HttpGet request = new HttpGet("http://192.168.1.97:89/Derdeery/Zaki.php");
 HttpResponse response = client.execute(request);
 HttpEntity entity=response.getEntity();
 data=EntityUtils.toString(entity);
 JSONArray json=new JSONArray(data);
 Log.e("STRING", data);
  } 
 catch(Exception e)
 {}
 return null;
  }
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
try
{
if (progressDialog.isShowing()) 
{
progressDialog.dismiss();
}
JSONArray json=new JSONArray(data);
for(int i=0;i<json.length(); i++)
{
String name=obj.getString("Part_NAME");
String id = obj.getString("Part_ID") ;
Log.e("name", name); 
r.add(id );
r2.add(name);
}
list.setAdapter(adapter);
list2.setAdapter(adapter2);
} 
catch (JSONException e)
 {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 }}
 }

2 个答案:

答案 0 :(得分:0)

您可以简单地创建TextView并将其添加为ListView的标题视图

TextView titleText = new TextView(this);
titleText.setText("Title ListView 1");
listView.addHeaderView(titleText);

答案 1 :(得分:0)

首先定义textview,然后将其添加到listview标题。

TextView textView = new TextView(MainActivity.this);
textView.setText("Title Here");

list.addHeaderView(textView);