Skip to content

Instantly share code, notes, and snippets.

@shobotch
Created February 7, 2014 13:04
Show Gist options
  • Save shobotch/8862300 to your computer and use it in GitHub Desktop.
Save shobotch/8862300 to your computer and use it in GitHub Desktop.
<?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="match_parent"
android:orientation="vertical"
android:id="@+id/main_frame" >
<TextView
android:id="@+id/screen_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/tweet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
package ws.temp.dustcode;
import java.util.LinkedList;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private final static String TAG = MainActivity.class.getSimpleName();
private ListView mListView;
MAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mListView = new ListView(this);
setContentView(mListView);
adapter = new MAdapter(this);
mListView.setAdapter(adapter);
for (int i = 0; i < 100; i++) {
adapter.addTweet(DummyTweet.getInstance(
"tweet No. " + String.valueOf(i), "cn", i));
}
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
TweetListHolder holder = (TweetListHolder) arg1.getTag();
Integer id = holder.tweetid;
DummyTweet t = adapter.getItemFromTweetId(id);
if (t != null)
Toast.makeText(getApplicationContext(),
"以下のツイートをふぁぼりました。\n" + t.tweet,
Toast.LENGTH_SHORT).show();
}
});
}
public static class MAdapter extends BaseAdapter {
private LinkedList<DummyTweet> tweetList = new LinkedList<MainActivity.DummyTweet>();
private LayoutInflater inflater;
public MAdapter(Activity context) {
this.inflater = context.getLayoutInflater();
}
public void addTweet(DummyTweet tweet) {
tweetList.addFirst(tweet);
notifyDataSetChanged();
}
@Override
public int getCount() {
return tweetList.size();
}
@Override
public DummyTweet getItem(int position) {
return tweetList.get(position);
}
public DummyTweet getItemFromTweetId(int id) {
for (int i = 0; i < tweetList.size(); i++)
if (tweetList.get(i).id == id)
return tweetList.get(i);
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// ここで各データを表示する処理を書く
TweetListHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.activity_main, null);
holder = new TweetListHolder();
holder.tweet = (TextView) convertView.findViewById(R.id.tweet);
holder.screenName = (TextView) convertView
.findViewById(R.id.screen_name);
convertView.setTag(holder);
} else {
holder = (TweetListHolder) convertView.getTag();
}
DummyTweet tweet = tweetList.get(position);
holder.tweetid = tweet.getId();
holder.screenName.setText(tweet.getScreenName());
holder.tweet.setText(tweet.getTweet());
return convertView;
}
}
public static class TweetListHolder {
public TextView tweet;
public TextView screenName;
public int tweetid;
}
public static class DummyTweet {
private String tweet;
private String screenName;
private int id;
public DummyTweet(String tweet, String screenName, int id) {
this.tweet = tweet;
this.screenName = screenName;
this.id = id;
}
public static DummyTweet getInstance(String tweet, String screenName,
int id) {
return new DummyTweet(tweet, screenName, id);
}
public String getTweet() {
return tweet;
}
public void setTweet(String tweet) {
this.tweet = tweet;
}
public String getScreenName() {
return screenName;
}
public void setScreenName(String screenName) {
this.screenName = screenName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment