我有一个listview使用游标填充,其中存在自定义行。行中有一组文本视图。我还有一个图像视图。我想根据textview的值设置图像。我的代码是
public class DisplayCards extends Activity {
private Divyadesamdb dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pasuram);
dbHelper = new Divyadesamdb(this);
dbHelper.open();
// Clean all data
dbHelper.deleteAllPasurams();
// Add some data
dbHelper.insertPasurams();
Intent intent = getIntent();
String category = intent.getStringExtra(MainActivity.EXTRA_CATEGORY);
String start = intent.getStringExtra(MainActivity.EXTRA_START);
String ending = intent.getStringExtra(MainActivity.EXTRA_ENDING);
// Generate ListView from SQLite Database
displayListView(category, start, ending);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void displayListView(String mcategory, String mstart, String mending) {
String category = mcategory;
String starting = mstart;
String ending = mending;
int rowsFound = 0;
Cursor cursor = dbHelper.fetchAllPasurams(category, starting, ending);
/*
* How many rows returned from the query
*/
rowsFound = cursor.getCount();
// the desired columns to be bound
String[] columns = new String[] {
Divyadesamdb.PASURAMS_COLUMN_PAASURAMNUMBER,
Divyadesamdb.PASURAMS_COLUMN_AAYIRAM,
Divyadesamdb.PASURAMS_COLUMN_AZHWAAR,
Divyadesamdb.PASURAMS_COLUMN_CATEGORY,
Divyadesamdb.PASURAMS_COLUMN_MANGALASASANAMON,
Divyadesamdb.PASURAMS_COLUMN_PAASURAM_EN_STR,
Divyadesamdb.PASURAMS_COLUMN_SUBCATEGORY,
Divyadesamdb.PASURAMS_COLUMN_TITLE,
Divyadesamdb.PASURAMS_COLUMN_TITLENUMBER };
// the XML defined views which the data will be bound to
int[] to = new int[] { R.id.tvPaasuramNumber, R.id.tvAayiram,
R.id.tvAzhwaar, R.id.tvCategory, R.id.tvMangalasasanamOn,
R.id.tvPaasuram, R.id.tvSubCategory, R.id.tvTitle,
R.id.tvtvTitleNumber };
// create the adapter using the cursor pointing to the desired data
// as well as the layout information
MyCursorAdapter dataAdapter = new MyCursorAdapter(this,
R.layout.paasuram_single_item, cursor, columns, to, 0);
ListView listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(dataAdapter);
}
// extend the SimpleCursorAdapter to create a custom class where we
// can override the getView to change the row colors
private class MyCursorAdapter extends SimpleCursorAdapter {
public MyCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// get reference to the row
View view = super.getView(position, convertView, parent);
// check for odd or even to set alternate colors to the row
// background
// want to get the value of this text view,
// but not getting handle.
LayoutInflater inflater = ((Activity) Context).getLayoutInflater();
TextView azhwaarView = (TextView) view.getTag(R.id.textViewAzhwaar);
String azhVal = azhwaarView.getText().toString();
if (position % 2 == 0) {
view.setBackgroundColor(Color.rgb(238, 233, 233));
} else {
view.setBackgroundColor(Color.rgb(255, 255, 255));
}
return view;
}
}
}
不确定如何获取textview的句柄。获取Null指针异常。非常感谢任何帮助。
paasuram_single_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/mybackground"
android:orientation="vertical" >
<ImageView
android:id="@+id/ivAzhwaar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="false"
android:layout_margin="10dp"
android:contentDescription="@string/Azhwaar"
android:padding="5dp"
android:src="@drawable/nammalwar" />
<TextView
android:id="@+id/tvtvTitleNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/ivAzhwaar"
android:layout_toRightOf="@+id/ivAzhwaar"
android:text="TitleNumber"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/tvHyphan1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/tvtvTitleNumber"
android:layout_alignBottom="@+id/tvtvTitleNumber"
android:layout_toRightOf="@+id/tvtvTitleNumber"
android:text=" "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignBaseline="@+id/tvHyphan1"
android:layout_alignBottom="@+id/tvHyphan1"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/tvHyphan1"
android:text="Title"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/tvAayiram"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tvtvTitleNumber"
android:layout_below="@+id/tvtvTitleNumber"
android:text="Aayiram"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/tvHyphan2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvtvTitleNumber"
android:layout_toRightOf="@+id/tvAayiram"
android:text=" - "
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/tvCategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/tvHyphan2"
android:layout_alignBottom="@+id/tvHyphan2"
android:layout_toRightOf="@+id/tvHyphan2"
android:text="Category"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/tvHyphan3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/tvCategory"
android:layout_alignBottom="@+id/tvCategory"
android:layout_toRightOf="@+id/tvCategory"
android:text=" - "
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/tvSubCategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/tvHyphan3"
android:layout_alignBottom="@+id/tvHyphan3"
android:layout_toRightOf="@+id/tvHyphan3"
android:text="SubCategory"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/tvPaasuram"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tvAayiram"
android:layout_below="@+id/tvAayiram"
android:padding="5dp"
android:text="paasuram"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/tvPaasuramNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/tvPaasuram"
android:layout_alignBottom="@+id/tvPaasuram"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="PaasuramNumber"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/tvAzhwaar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/tvMangalasasanamOn"
android:layout_alignBottom="@+id/tvMangalasasanamOn"
android:layout_alignLeft="@+id/tvPaasuram"
android:text="Azhwaar"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/tvMangalasasanamOn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tvCategory"
android:layout_below="@+id/tvPaasuram"
android:layout_centerHorizontal="true"
android:text="MangalasasanamOn"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
答案 0 :(得分:0)
只有两个错误:
1).You are not inflating any view here:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
View view = inflater.inflate(R.layout.custom_inflator_layout,parent,false);
TextView azhwaarView = (TextView) view.getTag(R.id.textViewAzhwaar);
String azhVal = azhwaarView.getText().toString();
return view;
}
2).You are not seting any text to textview ,thats y when you trying to get the text
it gives null pointer.
答案 1 :(得分:0)
为了解决这个问题,我已经完成了以下工作:
感谢您的帮助!欣赏所呈现的每一个帮助。再次非常感谢。