2016-03-21 58 views
-2

當我嘗試使用自定義適配器將值設置爲列表視圖時,它僅顯示哈希映射中最後輸入的值。哈希映射是靜態的。我不知道爲什麼我沒有得到哈希映射中用於哈希映射的那些鍵的所有值。 這裏我的代碼無法使用自定義適配器將值設置爲列表視圖

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; 
} 

請help.I使用替代方法,但不是worked.thanks提前

+0

把你的完整的活動代碼與oncreate方法,這似乎是一半的代碼。 –

+0

爲什麼你不把hashmap傳遞給適配器構造函數。 – Pankaj

+0

爲什麼不使用'SimpleCursorAdapter'? – pskink

回答

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")); 

你在數組列表,但您要適配器添加散地圖從哈希映射中獲取數據,而不是從數組列表中獲取數值,這將返回哈希映射對象,並從該對象中獲取您需要獲取鍵值的值。

希望這會有所幫助。

+0

我剛剛嘗試過,但是當我運行代碼時,它會在列表中顯示空文本。 – Bivin

+0

空數據表示根據您的數據大小獲取行數,現在您需要檢查在列表中輸入數據的時間,還請在while循環前放置c.moveToFirst()。 –

+1

我檢查了代碼。現在我可以將值打印到文本視圖中,但它仍然檢索所有列的哈希映射中最後輸入的值。請問,你有另一種方式來查看列表視圖中的值? – Bivin

相關問題