我正在尝试创建一个显示事件名称、日期和开始时间的自定义 ListView。
我能够创建 ListView,但是只显示事件的名称。我已经用列表适配器尝试了几次实验,并为每个项目制作了单独的 ArrayLists,但没有奏效。你能告诉我如何在displaylist()
打印出“事件日期”和“事件时间”的方法中获取for循环中的数据吗?我正在遵循的教程对所有不实用的数据进行硬编码。请点击此链接获取我的应用程序的图片以及我希望显示的内容(位于页面底部):查看图片
public class DataView extends ListActivity{
FileInputStream inputStream;
String s = "";
List<String> eventNames;
List<String>eventDate;
List<String>eventStart;
List<String>eventEnd;
List<String>eventLocation;
List<String>eventDelete;
@Override
protected void onCreate(Bundle pop) {
super.onCreate(pop);
setContentView(R.layout.columns);
eventNames = new ArrayList<String>();
eventDate = new ArrayList<String>();
eventStart = new ArrayList<String>();
eventEnd = new ArrayList<String>();
eventLocation = new ArrayList<String>();
eventDelete = new ArrayList<String>();
File myFile = new File(getApplicationContext().getFilesDir().getAbsolutePath()+ "/Test.txt");
DownloadFileAsync FileTask = new DownloadFileAsync();
FileTask.execute(myFile);
}// end of onCreate
private class DownloadFileAsync extends AsyncTask<File, Void, String> {
@Override
protected String doInBackground(File... params) {
// TODO Auto-generated method stub
File myFile = new File(getApplicationContext().getFilesDir().getAbsolutePath()+ "/Schedule.txt");
while(!myFile.exists()){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
File myDir = new File(getApplicationContext().getFilesDir().getAbsolutePath());
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(myDir + "/Schedule.txt"));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
s = br.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String result = s;
return result;
}// end of do in background
@Override
protected void onPostExecute(String result) {
JSONArray jsonArray;
try {
jsonArray = new JSONArray(result);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String eventname =jsonObject.getString("event_name");
String eventdate =jsonObject.getString("event_date");
String eventstart =jsonObject.getString("event_start");
String eventend =jsonObject.getString("event_end");
String eventlocation =jsonObject.getString("event_location");
String eventdelete =jsonObject.getString("event_delete_flag");
Log.v("event web address", eventname+ " "+ eventdate+ " "+eventstart+ " "+eventend+ " "+eventlocation+ " "+eventdelete);
eventNames.add(eventname);
eventDate.add(eventdate);
eventStart.add(eventstart);
eventEnd.add(eventend);
eventLocation.add(eventlocation);
eventDelete.add(eventdelete);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String[] EventName = new String[ eventNames.size()];
eventNames.toArray(EventName);
String[] EventDate = new String[ eventDate.size()];
eventDate.toArray( EventDate );
String[] EventStart = new String[ eventStart.size()];
eventStart.toArray(EventStart);
String[] EventEnd = new String[ eventEnd.size()];
eventEnd.toArray( EventEnd);
String[] EventLocation = new String[ eventLocation.size()];
eventLocation.toArray(EventLocation);
String[] EventDelete = new String[ eventDelete.size()];
eventDelete.toArray( EventDelete);
displaylist(EventName,EventDate,EventStart,EventEnd,EventLocation, EventDelete);
}// end of onPostExecute
}/// end of DownloadFileAsync
public void displaylist(String[] EventName, String[] EventDate, String[] EventStart, String[] EventEnd, String[] EventLocation, String[] EventDelete){
ArrayList<HashMap<String, String>> mylistData =new ArrayList<HashMap<String, String>>();
int x=(int)EventName.length;
for(int i=0; i<x; i++)
{
HashMap<String,String> map = new HashMap<String, String>();
//initialize row data
for(int j=0; j<1; j++)
{
map.put("Event Name", EventName[i]);
map.put("Event Date", EventDate[i]);
map.put("Event Time", EventStart[i]);
}
mylistData.add(map);
}
SimpleAdapter arrayAdapter = new SimpleAdapter(this, mylistData, R.layout.mylistrow,new String []{"Event Name", "Event Date","Event Time"} , new int[]{R.id.text1,R.id.text2,R.id.text3});
setListAdapter(arrayAdapter);
}
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Object o = this.getListAdapter().getItem(position);
String event = o.toString();
Toast.makeText(this, "You have chosen event: " + " " + event, Toast.LENGTH_LONG).show();
}
}
主要的.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2"
android:background="#000fff"
android:drawSelectorOnTop="false" >
</ListView>
<TextView
android:id="@id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFff00"
android:text="No data" />
</LinearLayout>
mylistrow.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="6dip"
android:paddingTop="4dip" >
<TextView
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#FFFF00"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textSize="12sp"
android:textStyle="bold" />
<TextView
android:id="@+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textStyle="italic"
android:typeface="sans" />
</LinearLayout>