ローダーでロードされたデータを表示SimpleCursorAdapter
するためにとを使用しています。ListView
中には0から3までcursor
のaのアイテムがあります。int
このintが0-1に等しいアイテムには、レイアウト(右揃え、1つの色)を設定し、2-3に等しいアイテムには、別のレイアウト(左揃え、別の色)を設定します。
much like a chat app, where sent messages are on the right and received ones are on the left.
それを行う簡単な方法はありますか?0-1でlayout_1を膨らませ、2-3でlayout_2を膨らませるスイッチのようなもの。
編集:入力しようとしているListFragmentのコードを追加しました。スイッチとして使用するintは、MyContentProvider.Data.E_TYPEです。私はそれのコツをつかむことができません、しかし多分誰かが私が書かなければならないことをはっきりと説明することができます!
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.corsalini.survcontr.MyContentProvider.Data;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.content.CursorLoader;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
public class FragEvents extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor>{
@Override
public void onPause() {
allRead();
super.onPause();
}
private static final int EVENTS_LOADER = 0x02;
// This is the Adapter being used to display the list's data.
SimpleCursorAdapter mAdapter;
// If non-null, this is the current filter the user has provided.
String mCurFilter;
@Override public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Give some text to display if there is no data. In a real
// application this would come from a resource.
setEmptyText(this.getString(R.string.perform_event));
// We have a menu item to show in action bar.
setHasOptionsMenu(true);
// Create an empty adapter we will use to display the loaded data.
mAdapter = new SimpleCursorAdapter(getActivity(),
android.R.layout.simple_list_item_2, null,
new String[] { MyContentProvider.Data.E_TEXT, MyContentProvider.Data.E_DATE,
MyContentProvider.Data.E_NUMBER, MyContentProvider.Data.E_TYPE },
new int[] { android.R.id.text1, android.R.id.text2 },
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
setListAdapter(mAdapter);
// Start out with a progress indicator.
setListShown(false);
// Prepare the loader. Either re-connect with an existing one,
// or start a new one.
getActivity().getSupportLoaderManager().initLoader(EVENTS_LOADER, null, this);
}
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_events, menu);
}
@Override public void onListItemClick(ListView l, View v, int position, long id) {
//TODO Insert desired behavior here.
Log.i("FragmentComplexList", "Item clicked: " + id);
}
// These are the Contacts rows that we will retrieve.
static final String[] SUMMARY_PROJECTION = new String[] {
MyContentProvider.Data.E_ID,
MyContentProvider.Data.E_DATE,
MyContentProvider.Data.E_NUMBER,
MyContentProvider.Data.E_TEXT,
MyContentProvider.Data.E_TYPE,
};
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return new CursorLoader(getActivity(), MyContentProvider.Data.CONTENT_URI_EVENTS,
SUMMARY_PROJECTION, null, null,
Data.E_ID + " DESC");
}
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
// Swap the new cursor in. (The framework will take care of closing the
// old cursor once we return.)
mAdapter.swapCursor(data);
// The list should now be shown.
if (isResumed()) {
setListShown(true);
} else {
setListShownNoAnimation(true);
}
}
public void onLoaderReset(Loader<Cursor> loader) {
// This is called when the last Cursor provided to onLoadFinished()
// above is about to be closed. We need to make sure we are no
// longer using it.
mAdapter.swapCursor(null);
}
public void deleteEvent(ContentResolver contentResolver,
long id){
String selection = Data.E_ID + "=";
String[] args = {String.valueOf(id)};
contentResolver.delete(Data.CONTENT_URI_EVENTS, selection, args);
}
public void allRead(){
ContentResolver contentResolver = getActivity().getContentResolver();
ContentValues contentValue = new ContentValues();
contentValue.put(Data.E_NUMBER, Data.RECEIVED_READ);
String selection= Data.E_TYPE+"=";
String[] args= {String.valueOf(Data.RECEIVED_UNREAD)};
contentResolver.update(Data.CONTENT_URI_EVENTS, contentValue, selection, args);
}
}
編集:私がそれを正しく理解した場合、私の最終的なEventsAdapter(SimpleCursorAdapterを拡張する)は次のようになります:
import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class EventsAdapter extends SimpleCursorAdapter {
private Context localContext;
public EventsAdapter(Context context, int layout, Cursor c, String[] from,
int[] to, int flags) {
super(context, layout, c, from, to, flags);
localContext = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Cursor c= getCursor();
c.moveToPosition(position);
if(convertView == null)
{
LayoutInflater layoutInflator = (LayoutInflater)localContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
switch (getItemViewType(position)){
case 0:
convertView = layoutInflator.inflate(R.layout.item_event_0, null);
break;
case 1:
convertView = layoutInflator.inflate(R.layout.item_event_1, null);
break;
case 2:
convertView = layoutInflator.inflate(R.layout.item_event_2, null);
break;
case 3:
convertView = layoutInflator.inflate(R.layout.item_event_3, null);
break;
}
}
switch (getItemViewType(position)){
case 0:
TextView date0=(TextView)convertView.findViewById(R.id.date0);
TextView text0=(TextView)convertView.findViewById(R.id.text0);
date0.setText(""+c.getString(c.getColumnIndex(Data.E_DATE)));
text0.setText(""+c.getString(c.getColumnIndex(Data.E_TEXT)));
break;
case 1:
TextView date1=(TextView)convertView.findViewById(R.id.date1);
TextView text1=(TextView)convertView.findViewById(R.id.text1);
date1.setText(""+c.getString(c.getColumnIndex(Data.E_DATE)));
text1.setText(""+c.getString(c.getColumnIndex(Data.E_TEXT)));
case 2:
TextView date2=(TextView)convertView.findViewById(R.id.date2);
TextView text2=(TextView)convertView.findViewById(R.id.text2);
date2.setText(""+c.getString(c.getColumnIndex(Data.E_DATE)));
text2.setText(""+c.getString(c.getColumnIndex(Data.E_TEXT)));
case 3:
TextView date3=(TextView)convertView.findViewById(R.id.date3);
TextView text3=(TextView)convertView.findViewById(R.id.text3);
date3.setText(""+c.getString(c.getColumnIndex(Data.E_DATE)));
text3.setText(""+c.getString(c.getColumnIndex(Data.E_TEXT)));
}
return convertView;
}
@Override
public int getItemViewType(int position) {
int type = 0;
int returnInt = 0;
Cursor c= getCursor();
c.moveToPosition(position);
type= c.getInt(c.getColumnIndex(Data.E_TYPE));
switch (type){
case Data.RECEIVED_READ:
returnInt=3;
case Data.RECEIVED_UNREAD:
returnInt= 2;
case Data.SENT_COMPLETED:
returnInt= 1;
case Data.SENT_PROGRESS:
returnInt= 0;
default:
returnInt=0;
}
return returnInt;
}
@Override
public int getViewTypeCount() {
return 4;
}
}