Android-如何将图像放置在两列列表的其中一列中,并将图像垂直居中



我有一个2列的行布局,看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:paddingTop="4dip"
     android:paddingBottom="6dip"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal"
     android:textSize="13sp">
     <TextView android:id="@+id/TRAIN_CELL"
         android:layout_width="275dip"
         android:layout_height="wrap_content"/>
  <ImageView android:id="@+id/TO_CELL"
         android:layout_width="25dip"
         android:layout_height="wrap_content"  
         android:layout_weight="1"
         android:gravity="center"
         android:src="@drawable/arrow_button"/>
</LinearLayout>

一旦我从数据库中提取项目,我就会在左列中显示它们,在右列中,我希望有一个带箭头的小图像,表示它是一个可点击的项目。但我不知道如何渲染图像。以下是我目前填充该列表的方式:

我有两个变量:

private List<HashMap<String, String>> fillMaps;
private SimpleAdapter adapter;

一开始我是这样设置的:

list = (ListView) findViewById(android.R.id.list);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
//HashMap<String, String> map = new HashMap<String, String>();
// My data
fillMaps = new ArrayList<HashMap<String, String>>();
adapter = new SimpleAdapter(this, fillMaps, R.layout.questions_list,
        new String[] {"train", "to"}, 
        new int[] {R.id.TRAIN_CELL,  R.id.TO_CELL});
// This was the middle item R.id.FROM_CELL,
list.setAdapter(adapter);       
list.setTextFilterEnabled(true);

当数据从服务器返回时,我填充如下列表:

if ( obj != null )
                        {
                            questions.clear();
                            for ( int i = 0; i < obj.length(); i++ )
                            {
                                HashMap<String, String> map = new HashMap<String, String>();
                                JSONObject o = obj.getJSONObject(i);
                                question_id = o.getString("question_id");
                                question = o.getString("question");
                                question_by_member_id = o.getString("member_id");
                                Question q = new Question ( );
                                q.setQuestion( question );                      
                                q.setQuestionId( question_id );
                                q.setQuestionByMemberId(question_by_member_id);
                                map.put("train", question);
                                //map.put("from", ">");
                                //map.put("to", ">");
                                fillMaps.add(map);
                                questions.add( q );                             
                            }
                            adapter.notifyDataSetChanged();
                        }

但是我没有得到要渲染的图像。我认为问题的一部分是我使用字符串作为列表所期望的数据,但我不确定如何处理图像。顺便说一句,图像总是相同的图像。

谢谢!!:)

这就是您想要的吗?

<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:paddingTop="4dip"
     android:paddingBottom="6dip"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal"
     android:textSize="13sp">
     <TextView android:id="@+id/TRAIN_CELL"
         android:layout_width="275dip"
         android:layout_height="wrap_content"/>
     <ImageView android:id="@+id/TO_CELL"
         android:layout_width="25dip"
         android:layout_height="wrap_content"  
         android:layout_weight="1"
         android:gravity="center"
         android:src="@drawable/arrow_button"/>
</LinearLayout>

添加
(对不起,有时我会错过晚上堆积的通知。我相信你应该能够在Stack Overflow上一个接一个或一次全部阅读消息/删除通知,就像收件箱一样…)

无论如何,如果TO_CELL的图像总是相同的,只需在XML中设置图像即可。将您的SimpleAdapter更改为:

adapter = new SimpleAdapter(this, fillMaps, R.layout.questions_list,
        new String[] {"train"}, 
        new int[] {R.id.TRAIN_CELL});

现在您的适配器只处理一个字符串,您可以将其简化为ArrayAdapter(如果需要的话)


简化示例
您还可以显示一个TextView,并显示一个小的"下一个"箭头,如下所示:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:drawableRight="@android:drawable/ic_media_play"
    android:padding="10sp"
    android:textAppearance="?android:attr/textAppearanceMedium" />

(LinearLayout和单独的ImageView并不是绝对必要的。我将其保存为list_item.xml。)这里有一个简单的预览,请理解我留下了很多非关键的行,以避免重复:

public class Example extends Activity {
    ArrayAdapter<String> adapter;
    List<String> list = new ArrayList<String>();
    ListView listView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.text, list);
        listView = (ListView) findViewById(R.id.list);
        listView.setAdapter(adapter);
        updateList();
    }
    public void updateList() {
        ...
        if ( obj != null ) {
            questions.clear();
            list.clear();
            for ( int i = 0; i < obj.length(); i++ ) {
                JSONObject o = obj.getJSONObject(i);
                ...
                question = o.getString("question");
                list.add(question);
            }
            adapter.notifyDataSetChanged();
        }
    }
}

希望这能有所帮助!

我认为最简单的方法是尝试根据您的需要扩展ArrayAdapter,而不是使用SimpleAdapter。您可以覆盖getView()方法,并按照需要填充列表!

这里有几个例子,这里和这里。

编辑

首先,您需要对xml 进行一些修改

<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:paddingTop="4dip"
 android:paddingBottom="6dip"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal"
 android:textSize="13sp">
     <TextView android:id="@+id/TRAIN_CELL"
     android:layout_width="275dip"
     android:layout_height="wrap_content"/>
 <ImageView android:id="@+id/TO_CELL"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:gravity="center"              
     android:background="#fff"/>
</LinearLayout>

在下面的代码中,为了简单起见,我使用了字符串数组列表。您将不得不用一个列表来替换它,该列表包含您用来填充列表的任何对象。(可能有问题?)

创建一个ExtendedAdapter并重写getView()方法以执行您想要的操作。这将在列表的每一行上创建一个TextView和一个ImageView,用ArrayList项目中的项目填充TextView,并在项目可单击的情况下显示图像。(你必须自己做验证,但应该不难弄清楚)

public class ExtendedAdapter extends BaseAdapter {
public static final String TAG = "TodoAdapter";
private Context mContext;
private int layoutResource;
private boolean clickable = false;
private List<String> items; // you can replace this list with whathever you need
                            // for simplicity i'm assuming you already have the strings 
                            // you need for the textviews
public ExtendedAdapter(Context context, int textViewResourceId,
        List<String> items) {
    this.items = items;
    this.mContext = context;
    this.layoutResource = textViewResourceId;
}
public int getCount() {
    return items.size();
}
public Object getItem(int position) {
    return position;
}
public long getItemId(int position) {
    return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(layoutResource, null);
    }
    ImageView arrow = (ImageView) v.findViewById(R.id.TO_CELL);
    TextView text = (TextView) v.findViewById(R.id.TRAIN_CELL);
    text.setText(items.get(position));
    if (clickable)  // here you have to do your check if it's clickable or not
        arrow.setBackgroundResource(R.drawable.ic_launcher);
    return v;
}
}

在您的活动中,只需设置列表适配器

public class MainActivity extends Activity {
ListView yourList;
ArrayList<String> itemList = new ArrayList<String>();
ExtendedAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    yourList = (ListView) findViewById(R.id.yourList);

    adapter = new ExtendedAdapter(this, R.layout.row_layout, itemList);
    yourList.setAdapter(adapter);
    updateItems();
}
public void updateItems(){
    //dummy method, you should fetch your data and populate your array in a separate thread 
    //or something
    itemList.add("Text 1");
    itemList.add("Text 2");
    itemList.add("Text 3");
    itemList.add("Text 4");
    itemList.add("Text 5");
    itemList.add("Text 6");     
    adapter.notifyDataSetChanged();
}     

}

这就是创建一个包含文本和图像的列表的方法。

祝你好运!

我认为您应该自定义列表适配器,这样会很容易。

问题出在Layout和Simple adpader上。

1.不要使用linearLayout android:textSize="13sp"将其用于TextView、

你可以使用以下代码的

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingBottom="6dip"
    android:paddingTop="4dip" >
    <TextView
        android:id="@+id/TRAIN_CELL"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight=".25"
        android:text="try out by yourself..."
        android:textSize="13sp" />
    <ImageView
        android:id="@+id/TO_CELL"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".05"
        android:gravity="center"
        android:src="@android:drawable/arrow_down_float" />
</LinearLayout>

2.问题出在SimpleAdapter中使用

adapter=新的SimpleAdapter(this,fillMaps,R.layout.questions_list,new String[]{"train"},new int[]{R.id.TRAIN_CELL});

最新更新