0

我将ListView使用安装在 Android 设备上的应用程序的图标和标签进行自定义我编写了一些代码,但我不知道为什么当我运行应用程序时我ListView是空的,这里是代码:

public class MainActivity extends Activity {

ArrayList<String> AppLabel = new ArrayList<String>();
ArrayList<Drawable> AppIcon = new ArrayList<Drawable>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final ListView MyListView = (android.widget.ListView) findViewById(R.id.listView1);
    EditText MyEditText = (android.widget.EditText) findViewById(R.id.textView1);
    CustomAdapter Adapter = new CustomAdapter(this, android.R.layout.simple_list_item_multiple_choice,
    MyEditText, AppLabel);
    MyListView.setAdapter(Adapter);




   PackageManager pm = this.getPackageManager();
   List<ApplicationInfo> apps = pm.getInstalledApplications(0);

    for(ApplicationInfo app : apps) {

    String Label = (String)pm.getApplicationLabel(app);
    AppLabel.add(Label);


    Drawable Icon = pm.getApplicationIcon(app);     
    AppIcon.add(Icon);
       }//End for

    Adapter.notifyDataSetChanged();

}//end onCreate


class CustomAdapter extends ArrayAdapter<string>{
public CustomAdapter(Context context, int resource,
            EditText myEditText, ArrayList<String> appLabel) {
        super(context, resource);
        }
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    if (row==null) {
        LayoutInflater Inflater = getLayoutInflater();
        row = Inflater.inflate(R.layout.item_layout, parent, false);    
    }//End if
    EditText MyEditText = (android.widget.EditText) findViewById(R.id.textView1);
    MyEditText.setText(AppLabel.get(position));
    ImageView MyImageView = (ImageView) findViewById(R.id.imageView1);
    MyImageView.setImageDrawable(AppIcon.get(position));
    return (row);
    }//End get view
  }//End CustomAdapter Class

 }//end Main Class
4

3 回答 3

0

从视图 converView 中获取参考

EditText MyEditText = (android.widget.EditText)converView.findViewById(R.id.textView1);
MyEditText.setText(AppLabel.get(position));
ImageView MyImageView = (ImageView)converView.findViewById(R.id.imageView1);
MyImageView.setImageDrawable(AppIcon.get(position));
于 2012-10-09T08:50:53.060 回答
0

使用您的数组列表的大小将 getCount() 添加到您的自定义适配器。

于 2012-10-09T08:48:19.657 回答
0

到这里

http://www.ezzylearning.com/tutorial.aspx?tid=1763429

于 2012-10-09T08:57:15.917 回答