Android ListActivity可以使用包含2个TextView的Custom ArrayAdapter吗?

时间:2015-03-29 11:48:34

标签: android listview textview android-arrayadapter

在Activity中使用带有多个TextView的自定义数组适配器很容易。

但ListActivity简化了很多事情,例如使用OnItemClickListner。

所以问题很简单:我可以在ListActivity中实例化具有多个TextView的自定义数组适配器吗?

这里是代码,它非常简化以用于测试目的。 所有XML数组都消失了,它们在Java中进行了硬编码。我想使用已经注释掉的代码。这个代码工作我想在MainActivity类中扩展ListActivity而不是Activity,可以这样做吗?

package com.example.t22c19s14listtest;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnItemClickListener {
//  public class MainActivity extends ListActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ListView list = (ListView)findViewById(R.id.listView1);
    int [] images = {R.drawable.i,R.drawable.j,};
    String[] titleArray = new String[] { "Android", "iPhone",  };
    String[] descriptionArray = new String[] { "This is Android", "This is  iPhone",  }; 
            MyAdapter adapter = new MyAdapter(this, titleArray, images, descriptionArray);      
           list.setAdapter(adapter);
           list.setOnItemClickListener(this);}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//String item = (String) getAdapter().getItem(position);
            Toast.makeText(this,  "Selected", Toast.LENGTH_LONG).show();}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true; }
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item); }}

适配器看起来像那样;)

package com.example.t22c19s14listtest;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyAdapter extends ArrayAdapter<String> {
    Context context;
    int[] images;
    String[] titleArray;
    String[] descriptionArray;
    String title;
    MyAdapter(Context c, String [] titles, int imgs[], String [] desc) {        
    super(c, R.layout.single_row , R.id.textView4, titles);
    this.context=c;
    this.images=imgs;
    this.titleArray=titles;
    this.descriptionArray=desc;}
    @Override
    public View getView (int position, View convertView, ViewGroup parent ) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View row = inflater.inflate(R.layout.single_row,parent, false);
    ImageView myImage =  (ImageView) row.findViewById(R.id.imageView1);
    TextView myTitle =  (TextView) row.findViewById(R.id.textView4);
    TextView myDescryption =  (TextView) row.findViewById(R.id.textView2);              
    myImage.setImageResource(images[ position] );
    myTitle.setText(titleArray[position]);
    myDescryption.setText(descriptionArray[position]);
    return row;  }}

1 个答案:

答案 0 :(得分:0)

我现在肯定知道答案是:

你不能在ListActivity中实例化具有多个TextView的自定义数组适配器,它必须是Activity;)