Twitter4j将状态设置为列表视图 [英] Twitter4j set status to listview
问题描述
我有一个名为 statusses
列表,它其中有被称为鸣叫所需的文本状态
。我想将状态设置为列表视图
。
公共类MainActivity延伸活动{ 我的ListView;
清单<状态> statusess;
ConfigurationBuilder CB;
twitter4j.Status STATUS3; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
// TODO自动生成方法存根
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); I =(ListView控件)findViewById(R.id.listView1); 新LongOperation()执行(); }私有类LongOperation扩展的AsyncTask<弦乐,太虚,字符串> { @覆盖
保护字符串doInBackground(字符串... PARAMS){ TwitterFactory TF =新TwitterFactory(cb.build());
叽叽喳喳叽叽喳喳= tf.getInstance(); 的String [] = SRCH新的String [] {奥巴马};
ResponseList<使用者>用户= NULL;
尝试{
用户= twitter.lookupUsers(查找);
}赶上(TwitterException E){
// TODO自动生成catch块
e.printStackTrace();
}
对于(用户用户:用户){
的System.out.println(朋友的名字+ user.getName()); //这个打印我的朋友的名字
如果(user.getStatus()!= NULL)
{
的System.out.println(朋友时间轴);
尝试{
statusess = twitter.getUserTimeline(user.getName());
}赶上(TwitterException E){
// TODO自动生成catch块
e.printStackTrace();
}
对于(twitter4j.Status STATUS3:statusess)
{
的System.out.println(status3.getText());
}
}
}
返回null;
} @覆盖
保护无效onPostExecute(字符串结果){ } @覆盖
在preExecute保护无效(){
} @覆盖
保护无效onProgressUpdate(虚空......值){
}
}
现在这个的System.out.println(status3.getText());
工程100%,鸣叫都出现在控制台
。但我怎么让他们到列表视图
在 onPostExecute
?
我试过
i.setAdapter(新ArrayAdapter< twitter4j.Status>(MainActivity.this,
android.R.layout.simple_list_item_1,statusess));
但我得到不需要大量的文字,我只STATUS3这是文本(鸣叫)的需要,这显示很多东西像鸣叫的ID,转推,追随者等。
我也试过这种
@覆盖
保护无效onPostExecute(字符串结果){
对于(twitter4j.Status STATUS3:statusess)
{
i.setAdapter(新ArrayAdapter< statusess>(MainActivity.this,
android.R.layout.simple_list_item_1,status3.getText()));
}
}
但没有工作,很多红线的xD
用户= twitter.lookupUsers(查找);
这给你的用户列表。您正在运行一个for循环通过列表迭代,不断变化的状态
在每次迭代。当for循环结束后,状态
保存状态列表(由返回twitter.getUserTimeline(user.getName())
)中的最后的用户。这真的是你想要的吗?
例如,可以显示正在使用 twitter.lookupUsers(查找)返回的用户列表
,在ListView。而且,在一个项目单击事件,显示的是的用户的状态列表。
一旦你决定了用户
对他们来说,你所需要的状态列表,请执行以下操作:
//声明与类范围的ArrayList
ArrayList的<串GT; statusListTextOnly;//在doInBackground初始化()
@覆盖
保护字符串doInBackground(字符串... PARAMS){
....
....
statusListTextOnly =新的ArrayList<串GT;(); //初始化状态为你决定了用户
statusess = twitter.getUserTimeline(user.getName()); //运行for循环,以填补statusListTextOnly
//我们将使用'statusListTextOnly'与一个ArrayAdapter
对于(twitter4j.Status STATUS3:statusess){
statusListTextOnly.add(status3.getText());
}
}//初始化/复位ArrayAdapter
@覆盖
保护无效onPostExecute(字符串结果){
i.setAdapter(新ArrayAdapter<串GT;(MainActivity.this,
android.R.layout.simple_list_item_1,statusListTextOnly));
}
与code,问题就出在这里:
i.setAdapter(新ArrayAdapter< twitter4j.Status>(MainActivity.this,
android.R.layout.simple_list_item_1,statusess));
传递给ArrayAdapter对象,用的toString()
显示的方法。现在,的toString()
是自定义对象大多是被覆盖的,供应提供对象本身的一个有意义的描述的目的。它可以被重写,以提供几乎任何类型的以字符串格式的信息。在状态
对象,的toString的情况下,()
返回比你需要的东西多一点。所以,我们提取使用状态#的getText()
相关信息并将其存储在一个单独的ArrayList。
您第二次尝试有问题还有:
@覆盖
保护无效onPostExecute(字符串结果){
对于(twitter4j.Status STATUS3:statusess){
i.setAdapter(新ArrayAdapter< statusess>(MainActivity.this,
android.R.layout.simple_list_item_1,status3.getText()));
}
}
下面,你ArrayAdapter的通用参数设置给变量('状态'):通用参数应该是一个类。接下来,你传递一个字符串
作为最后一个参数,而一个ArrayAdapter的构造可以乘坐对象的数组或一个ArrayList。第三,你正在创建ArrayAdapter的一个新实例并将其设置为ListView控件的for循环的每个迭代。这在逻辑上是不正确。你需要一个ArrayAdapter的一个实例,你只需要设置一次。
还有什么可以做的:
-
创建一个能提供新的功能,例如自定义ArrayAdapter:图像显示与文本一起。
-
钻进去BaseAdapter:高度可定制的,通用适配器
I have a list called statusses
and among it the text needed which are the tweets called status
. I want to set the status to listview
.
public class MainActivity extends Activity {
ListView i;
List<Status> statusess;
ConfigurationBuilder cb;
twitter4j.Status status3;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
i = (ListView) findViewById(R.id.listView1);
new LongOperation().execute("");
}
private class LongOperation extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
String[] srch = new String[] {"Obama"};
ResponseList<User> users = null;
try {
users = twitter.lookupUsers(srch);
} catch (TwitterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (User user : users) {
System.out.println("Friend's Name " + user.getName()); // this print my friends name
if (user.getStatus() != null)
{
System.out.println("Friend timeline");
try {
statusess = twitter.getUserTimeline(user.getName());
} catch (TwitterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (twitter4j.Status status3 : statusess)
{
System.out.println(status3.getText());
}
}
}
return null;
}
@Override
protected void onPostExecute(String result) {
}
@Override
protected void onPreExecute() {
}
@Override
protected void onProgressUpdate(Void... values) {
}
}
now this System.out.println(status3.getText());
works 100%, the tweets are showing in the console
. but how do I get them to the listview
in onPostExecute
?
I tried
i.setAdapter(new ArrayAdapter<twitter4j.Status>(MainActivity.this,
android.R.layout.simple_list_item_1, statusess));
But I get a lot of text not needed, I need only status3 which is text(tweet), this displays a lot of stuff like tweet id, retweet, followers, etc ..
I also tried this
@Override
protected void onPostExecute(String result) {
for (twitter4j.Status status3 : statusess)
{
i.setAdapter(new ArrayAdapter<statusess>(MainActivity.this,
android.R.layout.simple_list_item_1, status3.getText()));
}
}
but didn't work, a lot of red line xD
users = twitter.lookupUsers(srch);
This gives you a list of users. You are running a for-loop to iterate through the list, changing statuses
on each iteration. When the for-loop ends, statuses
holds the status list(returned by twitter.getUserTimeline(user.getName())
) of the last user. Is this really what you want?
For example, you can display the user list that is returned using twitter.lookupUsers(srch)
, in the ListView. And, on a item click event, display the status list for that user.
Once you decide on the user
for whom you need the status list, do the following:
// Declare an ArrayList with class scope
ArrayList<String> statusListTextOnly;
// Initialize it in doInBackground()
@Override
protected String doInBackground(String... params) {
....
....
statusListTextOnly = new ArrayList<String>();
// Initialize 'statuses' for the user that you have decided on
statusess = twitter.getUserTimeline(user.getName());
// Run a for-loop to fill 'statusListTextOnly'
// We will use 'statusListTextOnly' with the ArrayAdapter
for (twitter4j.Status status3 : statusess) {
statusListTextOnly.add(status3.getText());
}
}
// Initialize/reset ArrayAdapter
@Override
protected void onPostExecute(String result) {
i.setAdapter(new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, statusListTextOnly));
}
The problem with your code lies here:
i.setAdapter(new ArrayAdapter<twitter4j.Status>(MainActivity.this,
android.R.layout.simple_list_item_1, statusess));
Objects passed to an ArrayAdapter are displayed using the toString()
method. Now, toString()
is mostly overriden for custom objects and serves the purpose of providing a meaningful description of the object itself. It can be overriden to provide literally any kind of information in a String format. In case of Status
objects, toString()
returns a bit more than what you need. So, we extract the relevant info using Status#getText()
and store it in a separate ArrayList.
Your second attempt has problems as well:
@Override
protected void onPostExecute(String result) {
for (twitter4j.Status status3 : statusess) {
i.setAdapter(new ArrayAdapter<statusess>(MainActivity.this,
android.R.layout.simple_list_item_1, status3.getText()));
}
}
Here, you are setting the generic parameter of ArrayAdapter to a variable('statuses'): the generic parameter should be a class. Next, you pass a String
as the last argument, whereas an ArrayAdapter's constructor can either take an array of objects, or an ArrayList. Third, you are creating a new instance of your ArrayAdapter and setting it to the ListView on each iteration of the for-loop. This is logically incorrect. You need one instance of an ArrayAdapter and you only need to set it once.
What else can you do:
Create a custom ArrayAdapter that affords new functionality, for example: showing of images along with text.
Dig into BaseAdapter: Highly customizable, all-purpose adapter.
这篇关于Twitter4j将状态设置为列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!