如何使用Position或Id在ListAdapterView中获取Textview

时间:2016-05-26 17:55:23

标签: android listview

我正在制作像学生列表视图这样的项目。所有学生都在列表视图中。当我点击学生时,它显示为存在。问题是,当我向下滚动或更改视图时,相同的位置显示存在。我的代码中有什么问题?

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (data == null)
        return;
    switch (requestCode) {
        case requestcode:
            String FilePath = data.getData().getPath();
            try {
                if (resultCode == RESULT_OK) {
                    AssetManager am = this.getAssets();
                    InputStream inStream;
                    Workbook wb = null;
                    try {
                        inStream = new FileInputStream(FilePath);
                        wb = new HSSFWorkbook(inStream);
                        inStream.close();
                    } catch (IOException e) {
                        lbl.setText("First "+e.getMessage().toString());
                        e.printStackTrace();
                    }
                    XlsxCon dbAdapter = new XlsxCon(this);
                    Sheet sheet1 = wb.getSheetAt(0);
                    if (sheet1 == null) {
                        return;
                    }

                    dbAdapter.open();
                    dbAdapter.delete();
                    dbAdapter.close();
                    dbAdapter.open();
                    Excel2SQLiteHelper.insertExcelToSqlite(dbAdapter, sheet1);
                    dbAdapter.close();
                }
            } catch (Exception ex) {
                lbl.setText(ex.getMessage().toString() + "Second");
            }
            ArrayList<HashMap<String, String>> myList = controller
                    .getProducts();
            if (myList.size() != 0) {
                 lv = getListView();
                ListAdapter adapter = new SimpleAdapter(MainActivity.this, myList,
                        R.layout.v, new String[]{Company, Product,
                        Price},
                        new int[]{R.id.txtproductcompany, R.id.txtproductname,
                                R.id.txtproductprice});
                setListAdapter(adapter);

            }

    }

}
public class ListClickHandler implements AdapterView.OnItemClickListener{

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
       /// String selectedValue = (String) getListAdapter().getItem(position);

        TextView voter = (TextView) view.findViewById(R.id.voter);

        long x = id;
        Toast.makeText(MainActivity.this , x+"ha",Toast.LENGTH_SHORT).show();
        if(voter.getText() != "PRESENT") {
            voter.setText(position+"PRESENT");
            Toast.makeText(MainActivity.this,
                    "Vote casted", Toast.LENGTH_SHORT).show();
        }
        else{
            Toast.makeText(MainActivity.this,
                    "Already Casted", Toast.LENGTH_SHORT).show();
        }

    }

xml代码:

 <?xml version="1.0" encoding="utf-8"?>

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:orientation="vertical" >

 <RelativeLayout
    android:id="@+id/lvh"
    android:layout_width="fill_parent"
    android:layout_height="60dp"
    android:orientation="horizontal"
    android:scrollbars="horizontal"
    android:background="#ffe6e6e6"
    android:weightSum="3"
    android:clickable="false">
    <ImageView
        android:paddingLeft="4dp"
        android:layout_width="45dp"
        android:src="@mipmap/ic_launcher"
        android:layout_height="60dp"
        android:id="@+id/imageView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>
    <TextView
        android:id="@+id/txtproductcompany"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/imageView"
        android:layout_toRightOf="@+id/imageView"
        android:text="NAME"
        android:paddingLeft="4dp"
        android:textColor="#2e69a3"
        android:textSize="15sp"
        android:layout_alignParentTop="true"
        />
    <TextView
        android:id="@+id/txtproductname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="4dp"
        android:text="Father Name"
        android:textColor="#3b343b"
        android:textSize="13sp"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/imageView"
        android:layout_toEndOf="@+id/imageView" />
    <TextView
        android:id="@+id/txtproductprice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="33100-3449820-1"
        android:paddingLeft="4dp"
        android:textColor="#e44040"
        android:textSize="13sp"
        android:layout_alignParentBottom="true"
        android:layout_toRightOf="@+id/imageView"
        android:layout_toEndOf="@+id/imageView" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ali"
        android:id="@+id/voter"
        android:textColor="#82da24"
        android:textSize="20dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />


 </RelativeLayout>

 </LinearLayout>

1 个答案:

答案 0 :(得分:0)

而不是更改视图,更改列表中的文字并致电notifyDataSetChanged();

voter.setText(position+"PRESENT"); \\replace this with followinf=g

//somthing like
myList.get(position) = "PRESENT";
this.notifyDataSetChanged();