动态添加图像到android listView项目

时间:2014-03-23 19:44:47

标签: android listview imageview

我正在尝试动态地将图片添加到ListView行。我是否必须创建自定义光标或其他内容?我只是不知道在哪里放置代码来更改图像源。

感谢。

这是活动,

public class ViewPlayersActivity extends Activity {
    ListView nameList;
    MySQLiteAdapters adapter_ob;
    MySQLiteHelper helper_ob;
    SQLiteDatabase db_ob;
    Button registerBtn;
    Cursor cursor, cursor2, cursor3;
    String context;
    public static final String PREFS = "examplePrefs";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_players);
    nameList = (ListView) findViewById(R.id.listView);
    adapter_ob = new MySQLiteAdapters(this);
    Bundle extras = getIntent().getExtras();

    SharedPreferences exmaple = getSharedPreferences(PREFS, 0);
    context = exmaple.getString("teamName", "cant find team");

     String[] from = {helper_ob.KEY_FNAME, helper_ob.KEY_LNAME};
     int[] to = { R.id.tv_fname, R.id.tv_lname};
     cursor = adapter_ob.queryName(context);
     SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);

    nameList.setAdapter(cursorAdapter);
    nameList.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub
                Bundle passdata = new Bundle();
                Cursor listCursor = (Cursor) arg0.getItemAtPosition(arg2);
                int nameId = listCursor.getInt(listCursor
                        .getColumnIndex("p_id"));                   
                passdata.putInt("keyid", nameId);
                Intent passIntent = new Intent(ViewPlayersActivity.this,
                        EditPlayerActivity.class);
                passIntent.putExtras(passdata);
                startActivity(passIntent);
            }
        });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.view_players, menu);
    return true;
}

// method refreshes this view to show record updates & Deletes
public void onResume() {
    super.onResume();
    cursor.requery();

}

这是行的当前xml。我在这里添加静态图像,但我需要它是动态的。

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/listcontainer"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/tv_fname"
        android:layout_width="210dp"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tv_lname"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:paddingTop="15dp"
        android:layout_marginLeft="20dp" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/icon_center" />

</LinearLayout>

以下是添加几行后ListView的样子。

Screenshot of app showing problem

0 个答案:

没有答案