Android listView - 格式化特定项目

时间:2011-11-14 22:00:00

标签: android android-listview

所以我对android编程比较陌生,目前正在开发自定义listView。我想要实现的目标如下:

  1. 从数据库中检索文章列表。
  2. 从“类别”列中获取所有唯一值以创建列表 类别。
  3. 使用这些类别填充listView
  4. 为每个类别点击添加一个监听器,将用户带到 该类别的第一篇文章。
  5. 我已成功完成上述所有工作,但现在想要进一步发展。我在数据库中有一个状态列,其值为“已读”或“未读”。我想做的是以下几点:

    1. 如果某个类别中的所有文章都是“已阅读”,则会使该类别变灰 在列表视图中,忽略对该特定项目的点击。
    2. 我完全不知道如何在列表视图中格式化单个项目...任何建议?我的代码如下:

      start.java:

      public class Start extends Activity {
      
          private ListView mainListView ;  
          private ArrayAdapter<String> listAdapter ;  
      
          public static final String MYDATABASE_NAME = "questions.db";
          public static final String MYDATABASE_TABLE = "questions";
          public static final int MYDATABASE_VERSION = 1;
          public static final String KEY_ID = "_id";
          public static final String KEY_CONTENT = "category";
      
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.start);
      
      // Query database
      
          SQLiteDatabase db;
      
          db = openOrCreateDatabase(
              "questions.db"
              , SQLiteDatabase.CREATE_IF_NECESSARY
              , null
              );
          db.setVersion(1);
          db.setLocale(Locale.getDefault());
          db.setLockingEnabled(true);
      
          ArrayList<String> categoryList = new ArrayList<String>();
      
          Cursor cur = db.query("questions", null, null, null, null, null, null);
          cur.moveToFirst();
      
              while (cur.isAfterLast() == false) {
      
                  if (categoryList.contains(cur.getString(11))) {
                      // do nothing
                  } else {
                      // add to list
                      categoryList.add(cur.getString(11));
                  }
                  cur.moveToNext();
              }
              cur.close();     
      
              Collections.sort(categoryList);
              categoryList.add(0, "All");
      
              // Find the ListView resource.   
              mainListView = (ListView) findViewById( R.id.mainListView );  
      
              // Create ArrayAdapter using the category list.  
              listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, categoryList);    
      
              // Set the ArrayAdapter as the ListView's adapter.  
              mainListView.setAdapter( listAdapter );       
      
              mainListView.setTextFilterEnabled(true);
              mainListView.setOnItemClickListener(new OnItemClickListener() {
                      public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
                          String clickedCat = (String) mainListView.getItemAtPosition(position);
                          finish();
                          Intent myIntent = new Intent(getApplicationContext(), NextClass.class);
                          myIntent.putExtra("passedCategory", clickedCat);
                          myIntent.putExtra("startTrigger", "go");
                          startActivity(myIntent);
                      }
              });     
      }  
      }
      

      start.xml

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:background="@android:color/black">
      
      <TextView 
          android:text="@string/categories" 
          android:layout_width="fill_parent" 
          android:layout_height="@dimen/topbar_container" 
          android:background="@drawable/topgradient" 
          android:gravity="center_vertical|center_horizontal" 
          android:textColor="@color/primarytext" 
          android:textSize="@dimen/topbar_font" 
          android:textStyle="bold"
          android:shadowColor="#000000"
              android:shadowDx="1"
              android:shadowDy="1"
              android:shadowRadius="2"></TextView>
      
       <ListView android:layout_width="fill_parent"   
        android:layout_height="fill_parent"   
        android:id="@+id/mainListView"></ListView> 
      
      </LinearLayout>
      

      simplerow.xml

      <TextView xmlns:android="http://schemas.android.com/apk/res/android"  
       android:id="@+id/rowTextView"   
       android:layout_width="fill_parent"   
       android:layout_height="wrap_content"  
       android:padding="10dp"  
       android:textSize="16sp" android:textStyle="bold">  
      </TextView>  
      

3 个答案:

答案 0 :(得分:0)

您需要设置ViewBinder,然后调用listAdapter.setViewBinder。

答案 1 :(得分:0)

根据我对您的问题的理解,请尝试查看here。第一个答案看起来对你有用。

答案 2 :(得分:0)

您需要为此创建自定义适配器:

<强> CustomAdapter.class:

public class CustomAdapter extends SimpleCursorAdapter{

    Cursor dataCursor;
    LayoutInflater mInflater;
    Context context;
    ArrayList<String[]> arrayList;

    public static HashMap<Integer,String> myList=new HashMap<Integer,String>();    

    public CustomAdapter(Context context, int layout, Cursor dataCursor, String[] from,
                    int[] to) {
            super(context, layout, dataCursor, from, to);

            this.context=context;
            this.dataCursor = dataCursor;
            mInflater = LayoutInflater.from(context);             

    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

            final ViewHolder holder;

            if(convertView==null)
            {
                     convertView = mInflater.inflate(R.layout.custom_listitem, null);
                     holder = new ViewHolder();                          

                     holder.textview=(TextView)convertView.findViewById(R.id.textview);
                             holder.layout=(LinearLayout)findViewById(R.id.layout);                              
                     convertView.setTag(holder);
            }
            else
            {   
                holder=(ViewHolder)convertView.getTag();
            }

            dataCursor.moveToPosition(position);

            String id=Integer.toString(dataCursor.getInt(dataCursor.getColumnIndexOrThrow("_id")));
            myList.put(position, id);  

            holder.textview.setText(dataCursor.getString(11));
            if(dataCursor.getString(dataCursor.getColumnIndex("index")).equals("read"))
            {               
                holder.layout.setBackgroundColor(Color.GRAY);               
            }

            // other code according to the functionality you want    
            return convertView;
    }   

    static class ViewHolder
    {
            TextView textview;  
            LinearLayout layout;         
    } 
}

<强> custom_listitem.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"    
    android:background="#FFFFFF"  
    android:id="@+id/layout"  
    android:padding="5dip"     
    >
    <TextView  
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"    
        android:textSize="17dip"
        android:gravity="left"      
        android:padding="5dip"  
        android:id="@+id/textview" 
    /> 
</LinearLayout>

现在,在以下活动中使用它:

<强> MainActivity.class:

...
mainListView = (ListView) findViewById( R.id.mainListView ); 
Cursor cursor = db.query("questions", null, null, null, null, null, null);

CustomAdapter ca=new CustomsAdapter(context,R.layout.custom_listitem,cursor,new String[]{"id","index"},new int[]{R.id.textview,R.id.textview});

mainListView.setAdapter(ca);

mainListView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int position,
                    long id) {

                String id=CustomAdapter.myList.get(position);
                Cursor findIndex=db.query("questions", null, "_id="+id, null, null, null, null);
                findIndex.moveToFirst();
                if(findIndex.getString(findIndex.getColumnIndex("index").equals("unread"))
                {
                     // do something for "unread" categories 
                }   
        }
});
...

我不知道怎么回事,你可以禁用点击特定的列表项但但是,你可以在一定条件下做这些事情让用户感觉某些项目是不可点击的。