2012-05-08 3 views
0

У меня есть активность, которая содержит список. Этот список будет брать данные из SQLite:SQLite With List

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. Должен ли я изменить адаптер?

============================ ОБНОВЛЕНИЕ =============== ============

добавить это мой проект, но я не изменил любую вещь, только что добавили этот класс:

 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

Да. Адаптер курсора будет следить за вашими данными списка – Yahor10

ответ

1

Вы должны создать адаптер пользовательского массива и выборка данных из базы данных по строкам и заполнение в Listview методом getView.

Вот пример для адаптера пользовательского массива

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

Нет, вы можете сделать еще одну вещь, которая извлекает все данные в arraylist и заполняет эти данные в вашем списке с помощью адаптера. –

+0

можете ли вы проверить мое обновление – Maha