Tôi đã làm ứng dụng cho máy tính bảng Android. Trong đó, tôi cần hiển thị hai ListView. Một cho listview đơn giản và một cho listview tùy chỉnh. Khi tôi nhấp vào hàng listview đơn giản thì chi tiết đó phải được phân phối trong một listview tùy chỉnh khác. Đối với điều này, tôi đã được đưa mảnh để hiển thị cả hai danh sách xem trong màn hình duy nhất. Đối với Chế độ xem danh sách tùy chỉnh, tôi đã được thực hiện bộ điều hợp tùy chỉnh để liên kết dữ liệu tùy chỉnh. Nhưng khi tôi nhấn vào hàng xem danh sách đơn giản, ứng dụng hiển thị lỗi không phản hồi. Mã của tôi sẽ như thế này. Đối với đoạn của tôi chứa mã như thế nàyLàm thế nào để thiết lập ListView tùy chỉnh trong Fragment android?
public class listDetails extends Fragment{
private int nAndroids;
public static ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
ListDetailsAdapter adapter;
static final String KEY_Title = "title";//"item";
public listDetails() {
}
/**
* Constructor for being created explicitly
*/
public listDetails(int nAndroids) {
this.nAndroids = nAndroids;
}
/**
* If we are being created with saved state, restore our state
*/
@Override
public void onCreate(Bundle saved) {
super.onCreate(saved);
if (null != saved) {
nAndroids = saved.getInt("nAndroids");
}
}
/**
* Save the number of Androids to be displayed
*/
@Override
public void onSaveInstanceState(Bundle toSave) {
toSave.putInt("nAndroids", nAndroids);
}
/**
* Make a grid and fill it with n Androids
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saved) {
int n;
Context c = getActivity().getApplicationContext();
LinearLayout l = new LinearLayout(c);
String listitems[]=new String[nAndroids];
HashMap<String, String> map = new HashMap<String, String>();
map.put(KEY_Title, "Question1");
map = new HashMap<String, String>();
map.put(KEY_Title, "Question2");
map = new HashMap<String, String>();
map.put(KEY_Title, "Question3");
map = new HashMap<String, String>();
map.put(KEY_Title, "Question4");
menuItems.add(map);
for (n = 0; n < nAndroids; n++)
{
listitems[n] = "one"+n;
ListView list = new ListView(c);
adapter = new ListDetailsAdapter(this, menuItems);
list.setAdapter(adapter);
}
return l;
}
}
Và mã bộ chuyển đổi tùy chỉnh của tôi như thế này
public class ListDetailsAdapter extends BaseAdapter{
///private listDetails listactivity;
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ListDetailsAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.customlist, null);
TextView title = (TextView)vi.findViewById(R.id.txtTitle);
Button btnone = (Button)vi.findViewById(R.id.btnfirst);
Button btntwo = (Button)vi.findViewById(R.id.btnsecond);
Button btnthree = (Button)vi.findViewById(R.id.btnthird);
Button btnfour = (Button)vi.findViewById(R.id.btnfourth);
Button btnfive = (Button)vi.findViewById(R.id.btnfifth);
btnone.setOnClickListener(oneclick);
btntwo.setOnClickListener(twoclick);
btnthree.setOnClickListener(thrirdclick);
HashMap<String, String> mymap = new HashMap<String, String>();
mymap = data.get(position);
title.setText(mymap.get(listDetails.KEY_Title));
return vi;
}
private View.OnClickListener oneclick = new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Log.e("button clicked", "button one clicked");
}
};
}
hãy hướng dẫn cho tôi whats đi sai trong đoạn code trên.
Tôi không thể tìm thấy bất kỳ lỗi rõ ràng nào trong mã của bạn, nhưng nếu bạn sử dụng trình gỡ rối trong Eclipse và thực hiện bước sau khi đặt điểm ngắt câu lệnh Log.e() trong onClickListener, có lẽ bạn có thể xác định được vấn đề? – span
Tôi thấy bạn đang thêm ba onClickListners khác nhau. Chúng ở đâu và tại sao bạn muốn làm điều đó? btnone.setOnClickListener (oneclick); btntwo.setOnClickListener (twoclick); btnthree.setOnClickListener (thrirdclick); – Noloxs
Có, chúng tôi có thể thực hiện một phương pháp hành động để xử lý nhiều thao tác nút. Nhưng về vấn đề của tôi nó không phải là vấn đề. Chỉ tôi đang lấy như thế thôi. Nhưng tôi không thực hiện chức năng cho điều đó. – nari