无法使用自定义适配器将值设置为列表视图

时间:2016-03-21 06:11:45

标签: android listview android-viewholder

当我尝试使用自定义适配器将值设置为列表视图时,它仅显示哈希映射中最后输入的值。哈希映射是静态的。我不知道为什么我没有在hashmap中获取我在hashmap中使用的那些键的所有值。 这是我的代码

public class Nextclass extends Activity  {
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.nextclass);
list = (ListView)findViewById(R.id.list1);

  SelectItems();
  ViewItems();
}

 private List<Map<String, String>> SelectItems() {
// TODO Auto-generated method stub
try {

       datas = new ArrayList<Map<String, String>>();
    DatabaseHelper dbHelper = new     DatabaseHelper(this.getApplicationContext());
    newDB = dbHelper.getWritableDatabase();
    Cursor c = newDB.rawQuery("select distinct code ,desc ,name "
            + " from item", null);


        while (c.moveToNext()){


       String cod = c.getString(c.getColumnIndex("code"));
       datanums.put("code", cod);
        String desc1  = c.getString(c.getColumnIndex("desc"));
        datanums.put("desc", desc1);
        String name = c.getString(c.getColumnIndex("name"));
         datanums.put("name", name);
              datas.add(datanums);
        }

    } catch (SQLiteException se ) {
        Log.e(getClass().getSimpleName(), "Could not create or Open the database");
       } 
    return datas; 
}
private void ViewItems() {

// TODO Auto-generated method stub

arrTemp = new String[datas.size()];


MyListAdapter myListAdapter = new MyListAdapter();
list.setAdapter(myListAdapter);
Log.v("list itemm",datas.toString());


 }

public  class MyListAdapter extends BaseAdapter{

@Override
public int getCount() {
    // TODO Auto-generated method stub
    if(datas != null && datas.size() != 0){
        return datas.size();    
    }
    return 0;
}

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    final ViewHolder holder;
    if (convertView == null) {

        holder = new ViewHolder();
        LayoutInflater inflater = Orders.this.getLayoutInflater();
        convertView = inflater.inflate(R.layout.order_simple_row, null);
        holder.textView1 = (TextView) convertView.findViewById(R.id.name);
        holder.textView2 = (TextView) convertView.findViewById(R.id.code);
        holder.textview3 = (TextView) convertView.findViewById(R.id.desc);
         holder.editText1 = (EditText) convertView.findViewById(R.id.cases); 


        convertView.setTag(holder);

    } else {

        holder = (ViewHolder) convertView.getTag();
    }

    holder.ref = position;

     holder.textView1.setText(datanums.get("name"));
   holder.textview3.setText(datanums.get("code"));
    holder.textView2.setText(datanums.get("desc"));
     holder.editText1.setText(arrTemp[position]);


        holder.editText1.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub
                arrTemp[holder.ref] = arg0.toString();
            }
        });

        return convertView;
    }

 private class ViewHolder {

     public Object list;
    TextView textView1,textView2,textview3;
 EditText editText1,editText2;
     int ref;
    }


    return convertView;
}

private class ViewHolder {

 public Object list;
TextView textView1,textView2,textview3;
EditText editText1;
 int ref;
}

请帮忙。我使用了其他方法,但没有提前工作。谢谢提前

1 个答案:

答案 0 :(得分:0)

 holder.textView1.setText(datas.get(position).get("name"));
   holder.textview3.setText(datas.get(position).get("code"));
    holder.textView2.setText(datas.get(position).get("desc"));

你是在数组列表中添加哈希映射但是在适配器中你试图从哈希映射中获取数据,而不是你需要从arraylist获取值,它将返回hashmap对象,你需要从该对象获取你的钥匙的价值。

希望这会有所帮助。