2013-11-26 136 views
0

「從選擇項目」我在我的第一個Android應用程序工作,所以我需要一些幫助..列表視圖

我有一個列表視圖至極從SQLite數據庫主張的價值觀,我需要選擇一個項目,得到這是身份證,並開啓新的活動......如何做到這一點?

我有一個活動顯示物品清單,但我不知道如何選擇物品並用物品ID打開另一個活動。

package chintan.khetiya.sqlite.cursor; 

import java.util.ArrayList; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.View.OnClickListener; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 
import chintan.khetiya.sqlite.cursor.Main_Screen.Contact_Adapter; 
import chintan.khetiya.sqlite.cursor.Main_Screen.Contact_Adapter.UserHolder; 

public class home_screen extends Activity{ 

//Button add_btn; 
ListView Contact_listview; 
ArrayList<Contact> contact_data = new ArrayList<Contact>(); 
Contact_Adapter cAdapter; 
DatabaseHandler db; 
String Toast_msg; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.home); 
try { 
    Contact_listview = (ListView) findViewById(R.id.list_pedidos); 
    Contact_listview.setItemsCanFocus(false); 
    Contact_listview.setClickable(true); 

    Set_Referash_Data(); 

} catch (Exception e) { 
    // TODO: handle exception 
    Log.e("Error", "" + e); 
} 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.Clientes: 
      finish(); 
      startActivity(new Intent(this, Main_Screen.class)); 
      return true; 

     case R.id.Productos: 
      finish(); 
      startActivity(new Intent(this, Product_Screen.class)); 
      return true; 

     case R.id.Nuevo_Pedido: 
      finish(); 
      startActivity(new Intent(this, home_screen.class)); 
      return true; 

     case R.id.Nuevo_Pedido_dentro: 
      finish(); 
      startActivity(new Intent(this, nuevo_pedido.class)); 
      return true;  

     case R.id.salir: 
      finish(); 
      return true; 
     default: 
    return super.onOptionsItemSelected(item); 
} 
} 
public void Set_Referash_Data() { 
contact_data.clear(); 
db = new DatabaseHandler(this); 
ArrayList<Contact> contact_array_from_db = db.Get_Contacts(); 

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

    int tidno = contact_array_from_db.get(i).getID(); 
    String name = contact_array_from_db.get(i).getName(); 
    String mobile = contact_array_from_db.get(i).getPhoneNumber(); 
    String email = contact_array_from_db.get(i).getEmail(); 
    Contact cnt = new Contact(); 
    cnt.setID(tidno); 
    cnt.setName(name); 
    cnt.setEmail(email); 
    cnt.setPhoneNumber(mobile); 

    contact_data.add(cnt); 
} 
db.close(); 
cAdapter = new Contact_Adapter(home_screen.this, R.layout.listview_pedidos_row, 
    contact_data); 
Contact_listview.setAdapter(cAdapter); 
cAdapter.notifyDataSetChanged(); 
} 

public void Show_Toast(String msg) { 
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show(); 
} 

@Override 
public void onResume() { 
// TODO Auto-generated method stub 
super.onResume(); 
Set_Referash_Data(); 

} 

public class Contact_Adapter extends ArrayAdapter<Contact> { 
Activity activity; 
int layoutResourceId; 
Contact user; 
ArrayList<Contact> data = new ArrayList<Contact>(); 

public Contact_Adapter(Activity act, int layoutResourceId, 
    ArrayList<Contact> data) { 
    super(act, layoutResourceId, data); 
    this.layoutResourceId = layoutResourceId; 
    this.activity = act; 
    this.data = data; 
    notifyDataSetChanged(); 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View row = convertView; 
    UserHolder holder = null; 

    if (row == null) { 
    LayoutInflater inflater = LayoutInflater.from(activity); 

    row = inflater.inflate(layoutResourceId, parent, false); 
    holder = new UserHolder(); 
    holder.name = (TextView) row.findViewById(R.id.user_name_txt); 
    holder.email = (TextView) row.findViewById(R.id.user_email_txt); 
    holder.number = (TextView) row.findViewById(R.id.user_mob_txt); 
    //holder.edit = (Button) row.findViewById(R.id.btn_update); 
    //holder.delete = (Button) row.findViewById(R.id.btn_delete); 
    row.setTag(holder); 
    } else { 
    holder = (UserHolder) row.getTag(); 
    } 
    user = data.get(position); 
    //holder.edit.setTag(user.getID()); 
    //holder.delete.setTag(user.getID()); 
    holder.name.setText(user.getName()); 
    holder.email.setText(user.getEmail()); 
    holder.number.setText(user.getPhoneNumber()); 
    return row; 

} 

class UserHolder { 
    TextView name; 
    TextView email; 
    TextView number; 
    Button edit; 
    Button delete; 
} 

} 

}

回答

1

,你將需要設置setOnItemClickListener的ListView中獲得所選項目的詳細信息,並移動到下一個活動爲:

Contact_listview.setOnItemClickListener(new AdapterView.OnItemClickListener(){ 
     @Override 
     public void onItemClick(AdapterView<?> adapter, View arg1, 
                 int arg2,long arg3) { 

        int selected_row=arg2; 
        // get selected row data 
        Contact user=(Contact)adapter.getItem(selected_row); 

        //start next activity here 
     } 
    });