2012-05-08 17 views
0

我有一个包含List的活动。这份名单将SQLite的取数据:带列表的SQLite

public class RecentCases extends Activity { 

Button GoToCaseInfo, CreateNewFormB; 
RecentCaseClass recent1; 

     // the adapter class.. 
    class RecentCasesInfoAdapter extends ArrayAdapter<RecentCaseClass> 

    { 
    public RecentCasesInfoAdapter() { 

     super(RecentCases.this, R.layout.recent_cases_row); 
    } 

    public View getView(final int position, View convertView, 
      ViewGroup parent) 

    { 

     recent1 = this.getItem(position); 

     LayoutInflater inflater = getLayoutInflater(); 
     View row = inflater.inflate(R.layout.recent_cases_row, parent, 
       false); 
     // this is our row items.. 
     TextView recentName = (TextView) row 
       .findViewById(R.id.tvrecentName); 
     TextView recenInfo = (TextView) row.findViewById(R.id.recent_info); 
     ImageView recentImg = (ImageView) row.findViewById(R.id.list_image); 

     // TODO What's the info they want 
     // String CaseTime = recent1.getTime(); 
     // recentName.setText(recent1.getName()); 
     // recenInfo.setText("His/her age: " + recent1.getAge() + 
     // " year old" 
     // + " Lost sicnce :" + CaseTime); 

     String CasePicPath; 

     // TODO Linear or ?? 
     RelativeLayout rowLayout = (RelativeLayout) row.findViewById(R.id.row_layout); 
     rowLayout.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 
      // go to 
      Intent k = new Intent(RecentCases.this, Case_Information.class); 
      startActivity(k); 
     } 
    }); 

     return row; 

    } 
    } 

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

    // Moving to Create New Form Activity 
    CreateNewFormB = (Button) findViewById(R.id.cretnwfrmRC); 
    CreateNewFormB.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 

      Intent k = new Intent(RecentCases.this, CreateNewForm.class); 
      startActivity(k); 

     } 
    }); 

    // For list 
    // Intilaize 
    ListView RecentCasesListView = (ListView)  findViewById(R.id.recent_cases_list); 

    // create adapter 
    RecentCasesInfoAdapter recentCasesInfoAdapter = new RecentCasesInfoAdapter(); 

    // 1-First receives MMS OnReceive Class 2- Assign the MMS info to Static 
    // Array 3- Assign the array to the adapater 
    // for(MedicineClass m: Model.getMedList()) MedicineInfoAdapter.add(new 
    // MedicineClass(m)); 
    recentCasesInfoAdapter.add(recent1); 

    // after fill the adapter.. assign the list to the adapter 
    RecentCasesListView.setAdapter(recentCasesInfoAdapter); 

} 
    } 

,这是类

public class RecentCaseClass { 

private String pic; 
private String name; 
private String gender; 
private int age; 
private String clothes; 
private String MoreInfo; 
private String time; 
private String location; 
private int ID; 

public String getPic() { 
    return pic; 
} 

public void setPic(String pic) { 
    this.pic = pic; 
} 
public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public String getGender() { 
    return gender; 
} 

public void setGender(String gender) { 
    this.gender = gender; 
} 

public int getAge() { 
    return age; 
} 

public void setAge(int age) { 
    this.age = age; 
} 

public String getClothes() { 
    return clothes; 
} 

public void setClothes(String clothes) { 
    this.clothes = clothes; 
} 

public String getMoreInfo() { 
    return MoreInfo; 
} 

public void setMoreInfo(String moreInfo) { 
    MoreInfo = moreInfo; 
} 

public String getTime() { 
    return time; 
} 

public void setTime(String time) { 
    this.time = time; 
} 

public String getLocation() { 
    return location; 
} 

public void setLocation(String location) { 
    this.location = location; 
} 

public int getID() { 
    return ID; 
} 

public void setID(int iD) { 
    ID = iD; 
} 

    } 

的短信会来我的应用程序,然后我将SQLite中保存的话,我应该表现出它在我的应用程序

  1. 如何从SQLite的获取数据,并显示在列表中?
  2. 如果我删除SQLite的行信息如何删除该行中列表?
  3. 我应该更改适配器?

============================ UPDATE =============== ============

我加入这个到我的项目,但我没有改变任何事情刚刚将该类:

 public class RecentClassAdapter extends BaseAdapter{ 


     private RecentCases RecentCases; 
     static public List<RecentCaseClass> listOfRCases; 
     RecentCaseClass entry; 
     int caseId; 

     public RecentClassAdapter(RecentCases recentcases, List<RecentCaseClass> listOfCaseParameter) { 
      this.RecentCases = recentcases; 
      this.listOfRCases = listOfCaseParameter; 

     } 

     public int getCount() { 
      return listOfRCases.size(); 
     } 

     public Object getItem(int position) { 
      return listOfRCases.get(position); 
     } 

     public long getItemId(int position) { 
      return position; 
     } 

     public View getView(int position, View convertView, ViewGroup viewGroup) { 

      entry = listOfRCases.get(position); 
      caseId = entry.getID(); 

      if (convertView == null) { 

       LayoutInflater inflater = (LayoutInflater) RecentCases.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       convertView = inflater.inflate(R.layout.recent_cases_row, null); 
       } 

      // this is row items.. 
      // Set the onClick Listener on this button 
      //ConfExpandRegion = (Button) convertView.findViewById(R.id.expand); 
      //Button Cancelb = (Button) convertView.findViewById(R.id.cancelCase); 
      TextView RCase = (TextView) convertView.findViewById(R.id.tvrecentName); 
      RCase.setText(entry.getName()); 
      Toast.makeText(RecentCases, "inside getview" + entry.getAge(), 0).show(); 

     public void add(RecentCaseClass RCaseClass) { 
      // TODO Auto-generated method stub 
      listOfRCases.add(RCaseClass); 
     } 


    } 

} 
+1

一个例子。如果你想绑定列表sqlite的,你的情况,最好使用光标adapter.http://开发商。 android.com/reference/android/widget/CursorAdapter.html – Yahor10

+0

所以我应该改变我的适配器? – Maha

+0

yes.Cursor适配器将您的观察列表数据 – Yahor10

回答

1

你必须创建一个自定义的数组适配器从数据库中逐行读取数据并使用getView方法填充到ListView中。

下面是对自定义阵列适配器

http://android-er.blogspot.in/2010/06/custom-arrayadapter-with-with-different.html

http://www.ezzylearning.com/tutorial.aspx?tid=1763429&q=customizing-android-listview-items-with-custom-arrayadapter

+0

我应该更换我的适配器吗? – Maha

+0

不,你可以做更多的事情,即获取数组列表中的所有数据,并使用适配器将该数据填充到您的列表视图中。 –

+0

你可以检查我的更新 – Maha