2012-04-04 2 views
0

Я написал CustomAdapter, чтобы отобразить список хеш-ключей + значения в качестве списка. Но я застрял на линии ниже.customAdapter, как распечатать содержимое массива в TextView.setText() из getview

label.setText (my_VALUES [0])

класса два мой CustomAdapter и я передаю ему Hashmap

public MyCustomAdapter(DynamicLists dynamicLists, int row, HashMap<String,String> data) 

здесь я совмещаю пар ключ + значение, и я назначить их в массив называется my_VALUES. Но я не знаю, правильно ли это?

Как распечатать каждую пару KEY + VALUE в виде отдельного элемента в режиме просмотра. на данный момент я могу только использовать label.setText (my_VALUES [0])

Благодаря

Грэма пакет gb.org;

//lists are not checkboxes 
import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.Collection; 
import java.util.HashMap; 
import java.util.Iterator; 
import java.util.LinkedHashMap; 
import java.util.List; 
import java.util.Map; 
import java.util.Map.Entry; 
import java.util.Set; 

import android.app.ListActivity; 
import android.content.Context; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 


//class 1 
public class DynamicLists extends ListActivity { 



//class 2 
public class MyCustomAdapter extends BaseAdapter { 
    private HashMap<String,String> mData = new HashMap<String,String>(); 
    private String[] mKeys; 
    String[] my_VALUES = new String[100]; 



    public MyCustomAdapter(DynamicLists dynamicLists, int row, HashMap<String,String> data){ 
    mData = data; 
    mKeys = mData.keySet().toArray(new String[data.size()]); 
    Log.d(TAG, "INSIDE ADAPTER HELLO WORLD " + map.entrySet()); 


    String[] array = new String[map.size()]; 
    int count = 0; 
    String combined=""; 
    for(Map.Entry<String, String> entry : map.entrySet()){ 
     combined="A"+entry.getKey()+"B"+entry.getValue(); 

    my_VALUES[count]=combined; 
    Log.d(TAG, " my_VALUES " + my_VALUES); 
    count++; 


    } 


} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
// TODO Auto-generated method stub 
    String key = mKeys[position]; 
    String Value = getItem(position).toString(); 

    //do your view stuff here 
    Log.d(TAG, "inside getview "); 
    View row=convertView; 

if (row==null){ 
    LayoutInflater inflater=getLayoutInflater(); 
    row=inflater.inflate(R.layout.row, null); 
} 

TextView label =(TextView)row.findViewById(R.id.blocked); 
label.setText(my_VALUES[0]); //printing out the last value 

return row; 

} 
@Override 
public int getCount() { 

    // TODO Auto-generated method stub 
    return mData.size(); 
} 
@Override 
public Object getItem(int position) { 
    // TODO Auto-generated method stub 
    return mData.get(mKeys[position]); 
} 
@Override 
public long getItemId(int position) { 
    // TODO Auto-generated method stub 
    return position; 
} 


} //end of class 2 



private static final String TAG = "Example"; 


//class 1 


public static HashMap<String, String> map = new HashMap<String, String>(); 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //tie data to list, call constructor MyCustomAdapter 
    //populate the list. ArrayAdapter takes current class, layout and array 
    map.put("year", "Apple");  
    map.put("make", "Mango");  
    map.put("model", "Grape");  
    map.put("style", "Orange");  
    map.put("series", "Peach"); 
    Log.d(TAG, "HELLO WORLD " + map.entrySet());  
    //link to my adapter 
    setListAdapter(new MyCustomAdapter(DynamicLists.this, R.layout.row, map)); 

} 


@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
// TODO Auto-generated method stub 
//super.onListItemClick(l, v, position, id); 
String selection = l.getItemAtPosition(position).toString(); 
Toast.makeText(this, selection, Toast.LENGTH_LONG).show(); 


} 


} //end of class 1 

ответ

1

Посмотрите, работает ли это.

Я только изменил 2 вещи: 1) как создается my_VALUES 2) label.setText (my_VALUES [позиция]);

public class DynamicLists extends ListActivity { 



//class 2 
public class MyCustomAdapter extends BaseAdapter { 
    private HashMap<String,String> mData = new HashMap<String,String>(); 
    private String[] mKeys; 
    String[] my_VALUES; 


    public MyCustomAdapter(DynamicLists dynamicLists, int row, HashMap<String,String> data){ 
    mData = data; 

    my_VALUES = new String[mData.size()]; 


    mKeys = mData.keySet().toArray(new String[data.size()]); 
    Log.d(TAG, "INSIDE ADAPTER HELLO WORLD " + map.entrySet()); 


    String[] array = new String[map.size()]; 
    int count = 0; 
    String combined=""; 
    for(Map.Entry<String, String> entry : map.entrySet()){ 
     combined="A"+entry.getKey()+"B"+entry.getValue(); 

    my_VALUES[count]=combined; 
    Log.d(TAG, " my_VALUES " + my_VALUES); 
    count++; 


    } 


} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
// TODO Auto-generated method stub 
    String key = mKeys[position]; 
    String Value = getItem(position).toString(); 

    //do your view stuff here 
    Log.d(TAG, "inside getview "); 
    View row=convertView; 

if (row==null){ 
    LayoutInflater inflater=getLayoutInflater(); 
    row=inflater.inflate(R.layout.row, null); 
} 

TextView label =(TextView)row.findViewById(R.id.blocked); 
label.setText(my_VALUES[position]); //printing out the last value 

return row; 

} 
@Override 
public int getCount() { 

    // TODO Auto-generated method stub 
    return mData.size(); 
} 
@Override 
public Object getItem(int position) { 
    // TODO Auto-generated method stub 
    return mData.get(mKeys[position]); 
} 
@Override 
public long getItemId(int position) { 
    // TODO Auto-generated method stub 
    return position; 
} 


} //end of class 2 



private static final String TAG = "Example"; 


//class 1 


public static HashMap<String, String> map = new HashMap<String, String>(); 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //tie data to list, call constructor MyCustomAdapter 
    //populate the list. ArrayAdapter takes current class, layout and array 
    map.put("year", "Apple");  
    map.put("make", "Mango");  
    map.put("model", "Grape");  
    map.put("style", "Orange");  
    map.put("series", "Peach"); 
    Log.d(TAG, "HELLO WORLD " + map.entrySet());  
    //link to my adapter 
    setListAdapter(new MyCustomAdapter(DynamicLists.this, R.layout.row, map)); 

} 


@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
// TODO Auto-generated method stub 
//super.onListItemClick(l, v, position, id); 
String selection = l.getItemAtPosition(position).toString(); 
Toast.makeText(this, selection, Toast.LENGTH_LONG).show(); 


} 


} //end of class 1 
1

вы в Java просто сделать label.setText (mKeys [положение] + GetItem (позиция) .ToString());

+0

FANTASTIC IT WORKED THANKS ДЛЯ ВСЕХ ВАШЕЙ ПОМОЩИ – user803271

+0

мое удовольствие, если оно сработало, пожалуйста, также принимайте ответ. – MikeIsrael