2013-11-01 65 views
0

我正在開發一個應用程序,因爲標題說如何將listview的項目放入String數組,而不是將字符串數組放入listview,而是將listview放入字符串數組。如何將ListView項目放入字符串數組?

我一直在尋找這個,但我只發現將字符串數組項放入列表視圖。

請幫助我提前謝謝你。

要澄清此線程,問題是如何將listview項目放入String數組中。

謝謝。 :d

代碼

public class DailyPlanTab extends Activity implements OnItemClickListener { 

ListView dailyPlanList; 

ArrayList<DailyManager> taskList = new ArrayList<DailyManager>(); 
DatabaseDailyPlan db; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.dailyplan_layout); 

    dailyPlanList = (ListView) findViewById(R.id.lvDailyPlanList); 
    dailyPlanList.setOnItemClickListener(this); 

    ImageView add = (ImageView) findViewById(R.id.ivDailyPlanAdd); 

    add.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Intent newDailyIntent = new Intent(getApplicationContext(), 
        NewDailyPlan.class); 
      startActivity(newDailyIntent); 

     } 
    }); 
} 

@Override 
protected void onResume() { 
    // TODO Auto-generated method stub 
    super.onResume(); 
    taskList.clear(); 

    db = new DatabaseDailyPlan(getApplicationContext()); 
    db.getWritableDatabase(); 

    ArrayList<DailyManager> tempList = db.getTask(); 

    for (int i = 0; i < tempList.size(); i++) { 

     String getTask = tempList.get(i).getDaily_name(); 
     String getDate = tempList.get(i).getDaily_date(); 
     int getId = tempList.get(i).getDaily_id(); 

     DailyManager dm = new DailyManager(); 
     dm.setDaily_name(getTask); 
     dm.setDaily_date(getDate); 
     dm.setDaily_id(getId); 

     taskList.add(dm); 
    } 

    dailyPlanList.setAdapter(new ListAdapter(this)); 
    // db.close(); 

} 

public class ListAdapter extends BaseAdapter { 

    LayoutInflater inflater; 
    ViewHolder viewHolder; 

    public ListAdapter(Context c) { 
     // TODO Auto-generated constructor stub 

     inflater = LayoutInflater.from(c); 
    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return taskList.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 

     if (convertView == null) { 
      convertView = inflater.inflate(R.layout.row_checklist_item, 
        null); 

      viewHolder = new ViewHolder(); 
      viewHolder.taskTitle = (TextView) convertView 
        .findViewById(R.id.tvCheckListItem); 
      convertView.setTag(viewHolder); 

     } else { 
      viewHolder = (ViewHolder) convertView.getTag(); 
     } 

     viewHolder.taskTitle.setText("" 
       + taskList.get(position).getDaily_name()); 

     return convertView; 
    } 
} 

public class ViewHolder { 

    TextView taskTitle, taskDate; 
} 

@Override 
public void onItemClick(AdapterView<?> arg0, View arg1, int position, 
     long arg3) { 
    // TODO Auto-generated method stub 

    int taskId = taskList.get(position).getDaily_id(); 
    String taskName = taskList.get(position).getDaily_name(); 
    String taskDate = taskList.get(position).getDaily_date(); 

    Intent newPlan = new Intent(getApplicationContext(), DailyPlan.class); 
    newPlan.putExtra("task_id", taskId); 
    newPlan.putExtra("task_name", taskName); 

    startActivity(newPlan); 
} 

其次是項目的列表視圖

public class DailyPlan extends Activity implements OnItemClickListener { 

final ArrayList<DailyManager> savedItems = new ArrayList<DailyManager>(); 

ListView checkList; 

Boolean nextItem = false; 
TempManager tm; 
DatabaseTemp dbTemp; 
Intent i; 
int taskId = -1; 
String taskName = " ", taskDate = null; 

DatabaseDailyPlan db; 
DailyManager dm; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.saved_dailyplan); 

    checkList = (ListView) findViewById(R.id.lvCheckList); 
    // checkList.setOnItemClickListener(this); 

    try { 
     i = getIntent(); 
     taskId = i.getExtras().getInt("task_id"); 
     taskName = i.getExtras().getString("task_name"); 
     Toast.makeText(getApplicationContext(), "From new id is" + taskId, 
       5000).show(); 

    } catch (Exception e) { 

    } 

    Button addList = (Button) findViewById(R.id.bAddList); 

    addList.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      // openDialog("", false, -1); 
     } 
    }); 

    if (nextItem) { 
     // openDialog("", false, -1); 
    } 

} 

public void refresh() { 

    DailyPlan.this.onResume(); 
} 

@Override 
protected void onResume() { 
    // TODO Auto-generated method stub 
    super.onResume(); 
    savedItems.clear(); 

    dbTemp = new DatabaseTemp(getApplicationContext()); 
    dbTemp.getWritableDatabase(); 

    db = new DatabaseDailyPlan(getApplicationContext()); 
    db.getWritableDatabase(); 

    if (taskId != -1) { ///For Load 

     ArrayList<DailyManager> savedList = db.getList(taskId); 
     for (int i = 0; i < savedList.size(); i++) { 

      String savedListItems = savedList.get(i).getDaily_list(); 
      String savedListTitle = savedList.get(i).getDaily_name(); 
      String savedListDate = savedList.get(i).getDaily_date(); 
      int savedListId = savedList.get(i).getDaily_id(); 

      DailyManager dm = new DailyManager(); 
      dm.setDaily_list(savedListItems); 
      dm.setDaily_name(savedListTitle); 
      dm.setDaily_date(savedListDate); 
      dm.setDaily_id(savedListId); 

      savedItems.add(dm); 
     } 
    } else { ///For New 

    } 
    checkList.setAdapter(new ListAdapter(this)); 
} 

public class ListAdapter extends BaseAdapter { 

    LayoutInflater inflater; 
    ViewHolder viewHolder; 

    public ListAdapter(Context c) { 
     // TODO Auto-generated constructor stub 

     inflater = LayoutInflater.from(c); 
    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return savedItems.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 

     if (convertView == null) { 
      convertView = inflater.inflate(R.layout.row_checklist_item, 
        null); 

      viewHolder = new ViewHolder(); 
      viewHolder.checkListItem = (TextView) convertView 
        .findViewById(R.id.tvCheckListItem); 

      convertView.setTag(viewHolder); 

     } else { 
      viewHolder = (ViewHolder) convertView.getTag(); 
     } 

     viewHolder.checkListItem.setText(savedItems.get(position) 
       .getDaily_list() + position); 

     final int temp = position; 

     return convertView; 
    } 
} 

private class ViewHolder { 
    TextView checkListItem; 
} 

@Override 
public void onItemClick(AdapterView<?> arg0, View arg1, int item, long arg3) { 
    // TODO Auto-generated method stub 
    // openDialog(savedItems.get(item).getDaily_name(), true, 
    // savedItems.get(item).getDaily_id()); 
} 

}

+0

爲什麼你需要把列表視圖的項目中字符串數組以及如何填充listview? – Raghunandan

+0

我使用ListAdapter填充列表視圖 – ralphgabb

+0

發佈您的代碼,爲什麼您需要一個字符串數組? – Raghunandan

回答

0

首先要創建字符串數組像這樣並將其放置在裏面的信息活動聲明的頂部:

private String[] stringArray = new String[listView.getCount()]; 

然後,您需要使用listView.getCount()來查找列表中有多少項目,然後您需要遍歷列表並將所有值添加到您之前創建的數組中。這可以這樣做:

for(int i=0; i < listView.getCount(); i++) 
{ 
    View v = list.getChildAt(i); 
    TextView tv= (TextView) v.findViewById(R.id.item); 
    stringArray[i] = tv.getText(); 
} 

希望這可以幫助和工作!如果不是,請告訴我。

+0

您不能創建沒有大小的數組。將第一行更改爲使用「new String [listView.getCount()]」。 –

+0

的確如此。我忘了那個。您可以在方括號內將其設置爲0,也可以執行@RickFalck建議的操作。 – edwoollard

0

首先,你必須通過你的列 的長度創建數組,並創建一個for循環的每個值在陣列中分配如下

string[] selected_item = new string[listView.SelectedItems.Count]; 
    for (int i = 0; i < selected_item.Length; i++) 
    { 
     selected_item[i] = listView.SelectedItems[i].Text; 
    } 
相關問題